![]() |
Assign data variable in the constructor? - 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: Assign data variable in the constructor? (/showthread.php?tid=8422) |
Assign data variable in the constructor? - El Forum - 05-17-2008 [eluser]Taff[/eluser] Hey. I'm passing a couple of variables to a view using Code: $this->load->view('default_view',$data); of which one variable is a title Code: $data['title']="This is the title"; which works fine if I assign in the function (index() in this case) but I would like to set a default title in the constructor (I think it's called the constructor) Code: class Task extends Controller { I would have expected it to work...but I expect everything to work until I try it. Am I doing something wrong, is it not possible, or is there a workaround (I thought maybe $data['title']=$this->config->item("cool") ![]() Thanks for any help, Taff Assign data variable in the constructor? - El Forum - 05-17-2008 [eluser]codedrunk[/eluser] if you insist on having that value in the constructor then maybe try this: Code: class Tasks extends Controller { Assign data variable in the constructor? - El Forum - 05-17-2008 [eluser]Taff[/eluser] Great, thanks! Quote:if you insist on having that value in the constructorWouldn't you assign that in the constructor if it is only going to change on a couple of occasions? Cheers, Taff Assign data variable in the constructor? - El Forum - 05-17-2008 [eluser]gtech[/eluser] It makes sense to put variables in the constructor when you are sharing the variable across the class functions, if its scope is only within the function itself then it makes sense to put it in the function. Assign data variable in the constructor? - El Forum - 05-17-2008 [eluser]codedrunk[/eluser] [quote author="Taff" date="1211054420"]Great, thanks! Quote:if you insist on having that value in the constructorWouldn't you assign that in the constructor if it is only going to change on a couple of occasions? Cheers, Taff[/quote] Yep. What i do is make it a config variable and then in my header I have this.. Code: <title><?=$this->config->item('title').$extended_title;?></title> It's suits my applications to just append whatever after the page title. Cheers ![]() Assign data variable in the constructor? - El Forum - 05-17-2008 [eluser]Taff[/eluser] cool, Cheers! |