CodeIgniter Forums
Help! How to set some general (global) vaiables? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Help! How to set some general (global) vaiables? (/thread-32892.html)

Pages: 1 2


Help! How to set some general (global) vaiables? - El Forum - 08-09-2010

[eluser]mddd[/eluser]
When declaring properties in your class you can only use static values. You can't declare a property to have a value that comes from a function.
If you want to do that, you have to write:
Code:
class My_Controller extends Controller
{
   public $links;

   function __construct()
   {
      $this->links = $this->general->get_links();
   }
}



Help! How to set some general (global) vaiables? - El Forum - 08-09-2010

[eluser]gyo[/eluser]
nope, victorche Smile

You just have to define your variable in the class properties, like:

Code:
class MY_Controller extends Controller
{
     public $links;

     function MY_Controller()
     {
        parent::Controller();

        $this->load->model('general_model', 'general');
        $this->links = $this->general->get_links();
     }
}


Now, from every controller you'll be able to access $this->views without having to do anything.


Help! How to set some general (global) vaiables? - El Forum - 08-09-2010

[eluser]John_Betong[/eluser]
I think the information that can be stored immediately after declaring the class is limited.

Once the variable exists then I store data from the controller. In the view it is called by $this->variable.


Help! How to set some general (global) vaiables? - El Forum - 08-09-2010

[eluser]gyo[/eluser]
Actually, for strict PHP5 you should also change the function MY_Controller to function __construct as mddd suggested.


Help! How to set some general (global) vaiables? - El Forum - 08-10-2010

[eluser]victorche[/eluser]
Thank you, @all! It works like a charm ... One last question I have ... Now I am not duplicating this part of code in every controller:
Code:
$links = $this->general->get_links();
But I still have this line in my data array:
Code:
$data = array(
    'menu_links' => $this->links,
    // other data here ...
);
Is there an option to avoid this line in every controller too? I mean a chance to put it in MY_Controller.php? I really can not fully understand these parts, where some things will be used globally on every page. And the problem here is that I am using Phil's template library. So in MY_Controller.php I have:
Code:
$this->template->set_partial('header', 'header_tpl', FALSE); // This is the header partial
$this->template->set_layout('page_tpl'); // This is my main wrapper layout
But how can I insert $this->links in here? In the template library I have functions like "inject" which are maybe written for my case, but unfortunately the documentation is so poor :/ You can check it here:
http://philsturgeon.co.uk/code/codeigniter-template