Global variables in controllers - 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: Global variables in controllers (/showthread.php?tid=11513) |
Global variables in controllers - El Forum - 09-11-2008 [eluser]dpgtfc[/eluser] Hopefully I can explain this well, but it seems the controllers don't act as a regular class. I can't create a protected variable within the class and make setters and getters. The problem I have is the repetition of code. For example I have a controller named Home, of which has several functions for each page, "home", "about", "contact" etc. I'm also using the template class, though that doesn't matter too much I don't think. Anyway, on some of these I have several repeating variables, such as $data['content'] but some that change, such as $data['title']. I tried instantiating the array in the constructor, but that doesn't work. So like regular OO Php, I tried making a protected (switched to just var) outside of the constructor and tried using this: Code: class Home extends Controller{ Right now I have about 6 variables stored in the array, and on about half of the pages, 3-4 of the variable are the same. Some variables in the template I have blank, such as the admin menu which will only show up if you are logged in as admin. So I'd like to instantiate those variables as blank, such as $data['admin'] = ""; So how do I go about doing this, I've made my own classes before and I was always able to do things like $this->arr = $myArray; but I get "unexpected =" errors. Is it possible to create an assoc array that is used by all functions in a particular controller class? Or do I have to repeat my code in each function? This makes making any changes a bit tedious, as I have to change every function and doesn't seem very scalable. This is what OOP is supposed to take care of, right? Global variables in controllers - El Forum - 09-11-2008 [eluser]drewbee[/eluser] Create a configuration file. Would seem to work perfectly for this. Global variables in controllers - El Forum - 09-11-2008 [eluser]drewbee[/eluser] *DELETE* double post. Global variables in controllers - El Forum - 09-11-2008 [eluser]Sumon[/eluser] How is the idea if we use global varaibale and library like \application\libraries\common_tasks.php Code: class Common_tasks{ Code: class Home extends Controller{ Global variables in controllers - El Forum - 09-11-2008 [eluser]drewbee[/eluser] True. I didn't catch on to that (read it to fast). One other option is to actually extend the CI Controller. Code: class MY_Controller extends Controller Then we have our new controller: Code: class Home extends MY_Controller In the above controller, we now have access to myCommonVar by doing a $this->myCommonVar; Hope this makes sense. Global variables in controllers - El Forum - 10-04-2008 [eluser]nguyenbatbinh[/eluser] I don't understand used global variable in CI (used in all function in controller). I take a exemple like Sumon: Library: common_tasks.php Code: <?php Code: <?php Code: <?php It not work.. And a PHP Error was encountered Quote:Severity: Notice My code true or false, and can you tell me the way to fix and use global varialle. ____________________ Sorry! I speak english not good! Global variables in controllers - El Forum - 10-04-2008 [eluser]Sumon[/eluser] @nguyenbatbinh: would you please add Code: global $data; // automaticaly load default values Global variables in controllers - El Forum - 10-04-2008 [eluser]nguyenbatbinh[/eluser] Oh yes, It works! That mean, in each function if we want used $data['logged_in'] we must add global $data into the function? Thank you very much! Global variables in controllers - El Forum - 10-04-2008 [eluser]wiredesignz[/eluser] I'm sorry but globals are for lame coders, not for elite CI empowered programmers like us. Just because something works, it doesn't mean it's the right thing to do. Have a nice day :lol: |