How do I share data between my Controller's methods? - 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: How do I share data between my Controller's methods? (/showthread.php?tid=8706) |
How do I share data between my Controller's methods? - El Forum - 05-28-2008 [eluser]KeyStroke[/eluser] Hi, I'm looking for the best way to share common data between a controller's methods. Things like: 1. Logged in state 2. The user id of the person logged in (if any) 3. Date variable and so on. These are used in almost every method in my application. So far I've been doing something like this: Code: function page_name() But this isn't readable nor very practical since I have to call that function with every method I create. Any suggestions? How do I share data between my Controller's methods? - El Forum - 05-28-2008 [eluser]GSV Sleeper Service[/eluser] do something like this. Code: class foo extends Controller { How do I share data between my Controller's methods? - El Forum - 05-28-2008 [eluser]KeyStroke[/eluser] But where should I instantiate the data? in the constructor of the class? How do I share data between my Controller's methods? - El Forum - 05-28-2008 [eluser]GSV Sleeper Service[/eluser] yep, in the constructor How do I share data between my Controller's methods? - El Forum - 05-28-2008 [eluser]KeyStroke[/eluser] Ok thanks. One last question: Is the constructor called every time one of its methods is called? in other words, will the data in it be instantiated once in a session or every time a method is called? How do I share data between my Controller's methods? - El Forum - 05-28-2008 [eluser]m4rw3r[/eluser] It is called once per instantiation of the controller, which is every request. How do I share data between my Controller's methods? - El Forum - 05-28-2008 [eluser]KeyStroke[/eluser] Thank you very much GSV and m4rw3r (man, that's more like a password !!) for your replies. How do I share data between my Controller's methods? - El Forum - 05-28-2008 [eluser]onejaguar[/eluser] Although it probably isn't as "clean" as setting a global variable for your class, I get tired of writing $this-> all the time so I often just pass the variable as a reference; e.g.: Code: function page_name() |