![]() |
How to call a variable globally - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: How to call a variable globally (/showthread.php?tid=3779) |
How to call a variable globally - El Forum - 10-21-2007 [eluser]cinewbie81[/eluser] Hi guys, I have a file ‘globalsetting.php’ as following: Code: <?php How can i call variables DB1, DB2, and MyClient from other controller class ?? Anyone ? IS that correct that I put Globalsetting class extend from controller ? Hope someone can helkp as I’m stuck here for quite a while .. thanks in advance How to call a variable globally - El Forum - 10-21-2007 [eluser]Colin Williams[/eluser] Controllers handle your application logic and are typically mapped to a specific URL. Models interact with the database so your controller doesn't have to. Then, views handle the presentation. By your code, the 'Initialize' method is mapped to mysite.com/globalsetting/Initialize. CI provides config files, Libraries, Helpers, and Models for interaction on a global scope. please read through the first part of the user guide to learn more about CI's approach. How to call a variable globally - El Forum - 10-21-2007 [eluser]thurting[/eluser] I don't really know what you are trying to accomplish with that class, but it doesn't look like it should be a subclass of Controller. In any case, if you want to define a global constant you should check out the define() function which is native to PHP. You could also define your global variables as static properties of a class. I would suggest coding in PHP5 if you were going to go this route. For example, you could create a class (GlobalSettings) with a static property (db) and access it via GlobalSettings::db. Depending on your application, you may want to make that class a singleton. You could also store your variables in SESSION, but you probably wouldn't want to do that. How to call a variable globally - El Forum - 10-22-2007 [eluser]cinewbie81[/eluser] [quote author="thurting" date="1193050783"]I don't really know what you are trying to accomplish with that class, but it doesn't look like it should be a subclass of Controller. In any case, if you want to define a global constant you should check out the define() function which is native to PHP. You could also define your global variables as static properties of a class. I would suggest coding in PHP5 if you were going to go this route. For example, you could create a class (GlobalSettings) with a static property (db) and access it via GlobalSettings::db. Depending on your application, you may want to make that class a singleton. You could also store your variables in SESSION, but you probably wouldn't want to do that.[/quote] I hope i got what u mean .. DO u mean create a class call GlobalSettings without extend from anything (not controller, model etc) like following: Class GlobalSettings() { // Declare my variable here } Where should i put this file ? Under controller folder ? And how can i call it from other controller class ? Sorry I'm quite new to php ![]() Some sample code will be appreciated thx How to call a variable globally - El Forum - 10-22-2007 [eluser]sharpe[/eluser] Try to read more about hooks. I thing this is what you want. How to call a variable globally - El Forum - 10-22-2007 [eluser]Michael Wales[/eluser] For global vars - I would just use a custom configuration file. How to call a variable globally - El Forum - 10-22-2007 [eluser]cinewbie81[/eluser] [quote author="walesmd" date="1193053620"]For global vars - I would just use a custom configuration file.[/quote] hi, cheers .. but what if the value of that variable is actually get from the database ? How to call a variable globally - El Forum - 10-22-2007 [eluser]Michael Wales[/eluser] You could do it with hooks, but those are hard for others to understand as there isn't a prevalent code trail to follow. Maybe create a custom controller and then have all of your app controllers extend the custom controller. How to call a variable globally - El Forum - 10-22-2007 [eluser]thurting[/eluser] If you stick you custom class in a folder called application/libraries you can load/instantiate the class using CI's built in load method. You could also just include a require statement at the top of the script using the class and then you could place it anywhere. How to call a variable globally - El Forum - 10-22-2007 [eluser]cinewbie81[/eluser] thx walesmd, thurting, sharpe and colin .. it's now SOLVED ![]() |