Setting a global variable in a controller? - 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: Setting a global variable in a controller? (/showthread.php?tid=10647) |
Setting a global variable in a controller? - El Forum - 08-07-2008 [eluser]Matrices[/eluser] Hello fellow CI-ers! I need to set a global variable within a controller that all the functions can access. The variable is $omit. The code is below. As you can see the $omit variable needs to be plugged into each function right now. This variable will be the same no matter what, so is there a way to set it globally, but only inside this specific controller? Code: <?php Setting a global variable in a controller? - El Forum - 08-07-2008 [eluser]usmc[/eluser] Like any object you can set properties. Code: $this->some_property = "Hello World"; Then access it from any method. Setting a global variable in a controller? - El Forum - 08-07-2008 [eluser]Bramme[/eluser] Yup. This is basic OO PHP... I suggest you read up on it a bit, it's definately worht it! Setting a global variable in a controller? - El Forum - 08-07-2008 [eluser]Matrices[/eluser] Ok I've replaced function Admin() with this code, and removed the $omit definitions from the individual functions: Code: function Admin() However I'm still receiving an error that omit is not defined. Setting a global variable in a controller? - El Forum - 08-07-2008 [eluser]Matrices[/eluser] I got it, thanks for the help. I realized that I still need to define $omit inside the functions using Code: $omit = $this->omit; I'm as green as sea glass, but I'm learnin'. Thanks for the help! Setting a global variable in a controller? - El Forum - 08-07-2008 [eluser]Hannes Nevalainen[/eluser] You can do this to: Code: //Constructor EDIT: Corrected the comment ('views' instead of 'controllers') Setting a global variable in a controller? - El Forum - 08-08-2008 [eluser]xwero[/eluser] First of all if you want to add a class variable you should define it in the top of your class Code: class somename When you define the variable it's possible to add a default value like you do with normal values. Matrices in your methods you can use $this->omit in place of the $omit variable name. Hannes the load->vars method is meant to collect variables for the view files not as a substitute for class variables. Setting a global variable in a controller? - El Forum - 08-08-2008 [eluser]Hannes Nevalainen[/eluser] xwero > I know, but he sends it to the view in every controller so I don't think it's such an bad idea. I wrote something wrong in my post, the comment in the code should say 'views' instead of 'controllers' Setting a global variable in a controller? - El Forum - 08-08-2008 [eluser]srobet[/eluser] [quote author="xwero" date="1218194148"] When you define the variable it's possible to add a default value like you do with normal values. Matrices in your methods you can use $this->omit in place of the $omit variable name. Hannes the load->vars method is meant to collect variables for the view files not as a substitute for class variables.[/quote] Yups, I agree with that Setting a global variable in a controller? - El Forum - 08-08-2008 [eluser]xwero[/eluser] Hannes i think you are looking at the wrong code Code: // Get the table fields |