![]() |
Variable Scope - Class Wide Variable - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Variable Scope - Class Wide Variable (/showthread.php?tid=23193) |
Variable Scope - Class Wide Variable - El Forum - 10-03-2009 [eluser]Unknown[/eluser] Hi, My controller currently has 6 functions (may be more later), and each function creates a variable called $data['categories']. This variable contains the same data each time, so my question is can I set the $data['categories'] variable at the class (controller) level once, and then access it in any of the controller's methods. Controller Code: Code: $data['categories'] = $this->MCategory->getAllCategories(); Many thanks Variable Scope - Class Wide Variable - El Forum - 10-03-2009 [eluser]Yorick Peterse[/eluser] Code: <?php Variable Scope - Class Wide Variable - El Forum - 10-03-2009 [eluser]Unknown[/eluser] Thanks Yorick, Had a play with your suggestion, but got an error when using private $data;, but not when using var $data; Looked into it, and realised it's because my server's running PHP4. Also, putting the code in the __construct() function didn't work, but again figured that was a PHP5 thing. Converted to PHP4 and it works a treat. Categories Controller Code: Code: class Categories extends Controller { Many thanks Variable Scope - Class Wide Variable - El Forum - 10-03-2009 [eluser]Yorick Peterse[/eluser] No problem ![]() |