![]() |
$this->load->vars() - 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: $this->load->vars() (/showthread.php?tid=13351) |
$this->load->vars() - El Forum - 11-19-2008 [eluser]kidego32[/eluser] Hello, I have the following code: Code: Class MY_Controller extends Controller { I'm extending my application controllers from this MY_Controller. The problem is that I can't access this data in the index() function. Shouldn't this data be available, or am I going about this the wrong way? Julio $this->load->vars() - El Forum - 11-19-2008 [eluser]sophistry[/eluser] from the manual: Quote:$this->load->vars($array) i believe that you only get the variables when you are in the view context (view has been "loaded"). if you want to share data through extended classes you should probably use class variables (var $title = 'title here' ![]() $this->load->vars() - El Forum - 11-19-2008 [eluser]kidego32[/eluser] Ok, thanks. $this->load->vars() - El Forum - 11-20-2008 [eluser]mihailt[/eluser] hmm.. maybe store'em in a static class variable. so you could access it via parent::$YourData ??? $this->load->vars() - El Forum - 11-20-2008 [eluser]xwero[/eluser] I think a class variable is a bad idea because then it's a part of the super object. Because you don't know how all the classes are named that can be added to the super object it's possible you get same name errors. A way to get the stored vars is to extend the loader class with following method: Code: function var($key) Code: $this->load->var('some_var'); $this->load->vars() - El Forum - 11-20-2008 [eluser]mihailt[/eluser] [quote author="xwero" date="1227205020"]I think a class variable is a bad idea because then it's a part of the super object. Because you don't know how all the classes are named that can be added to the super object it's possible you get same name errors. [/quote] not sure that i got you right , but i think it's not the issue, since static variable is a class variable not object variable and object instance doesn't have acces to it. Anyway, setting the implementation aside, what needs to be done can be achived by following registry pattern. $this->load->vars() - El Forum - 11-20-2008 [eluser]xwero[/eluser] Php doesn't make care what type the object is, that is a logical difference. So if you add the variable with the name vars but if you load a library with the same name it's likely somewhere down the line you get bugs. The loader already keeps an array of the variables for the view files so i don't see why you need another way to store them. $this->load->vars() - El Forum - 11-20-2008 [eluser]mihailt[/eluser] [quote author="xwero" date="1227207794"]Php doesn't make care what type the object is, that is a logical difference. So if you add the variable with the name vars but if you load a library with the same name it's likely somewhere down the line you get bugs. The loader already keeps an array of the variables for the view files so i don't see why you need another way to store them.[/quote] Agree with about CI_Loader, but i still don't think there can be any conflicts since $someobject->vars, $someobject::$vars and $someobject->vars() are pretty obvious different things, and what i suggested was to use static variable, static variables belong to the class and not to the instance of the class, you cannot access it within an object scope. About the logic part it's actaully not a big difference - both are libraries so it doesn't matter much, the only difference is that in one case you would store variables in parent class and in other case you would store it in superobject. |