![]() |
Constructor for libraries - 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: Constructor for libraries (/showthread.php?tid=26092) |
Constructor for libraries - El Forum - 01-05-2010 [eluser]Random dude[/eluser] Because you have to $CI =& get_instance(); in each function, and i heard you can have constructors in libraries, I thought I should try it. But my current code isn't working Also, what syntax can I use to call functions that are within the safe library? $CI? $this? This is the library. Code: class General_Functions { Thank you. For now I've just gone back to initilizing $CI in each function in the library. Constructor for libraries - El Forum - 01-05-2010 [eluser]Cro_Crx[/eluser] $CI is an instance variable. To access the class variable $CI up the top you need to use $this->CI so your code should go like this if you want to reuse $CI all over Code: class General_Functions { The user guide says that this won't with with PHP4 (as it's old). Here's a part of the user guide. "Also, please note: If you are running PHP 4 it's usually best to avoid calling get_instance() from within your class constructors. PHP 4 has trouble referencing the CI super object within application constructors since objects do not exist until the class is fully instantiated." Constructor for libraries - El Forum - 01-06-2010 [eluser]louis w[/eluser] I have this in a helper file which get autoloaded Code: function CI() { You use it like this: Code: CI()->something->whatever(); I find that it helps quite a lot when working with libraries, or even allowing models to talk to each other. Constructor for libraries - El Forum - 01-06-2010 [eluser]wowdezign[/eluser] louis w, What about libraries loading other libraries? Does it help there? Are there any downsides? Constructor for libraries - El Forum - 01-06-2010 [eluser]louis w[/eluser] You could use this to perform any CI action, so loading libraries from inside other libraries is totally possible. I have never needed to do this thou. Constructor for libraries - El Forum - 01-06-2010 [eluser]wowdezign[/eluser] Maybe it isn't good practice, I don't know. But I am working on an Availability Calendar class right now that displays 3 Calendars at once. The approach I took was to load the Calendar library into my custom library so mine could have three instances on it. Maybe I missed a better way of doing it though. So, I guess then, that you haven't found any downsides... Hmmm... I'll have to try that. |