![]() |
[Solution] Extending libraries: proper locations for the Classname and MY_Classname library files - 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: [Solution] Extending libraries: proper locations for the Classname and MY_Classname library files (/showthread.php?tid=24249) |
[Solution] Extending libraries: proper locations for the Classname and MY_Classname library files - El Forum - 11-04-2009 [eluser]aidehua[/eluser] This is probably second nature to most of you, but I've just spent an hour trying to figure it out so I thought I'd post it here in case it might save someone else five minutes down the line. (I was using Simon Maddox's Twitter class (available at http://github.com/simonmaddox/codeigniter-twitter), but I think this would apply to any library class in CodeIgniter.) First up I downloaded the class file (Twitter.php) and saved it in my application/libraries folder. I was able to load it successfully from the controller in the usual way: Code: $this->load->library('twitter'); But then I wanted to extend the class a little bit, so I created a new class and called it MY_Twitter.php (as documented at http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html). I saved my new class in the application/libraries folder. I then tried to load the extended library from the controller, again as per the documentation, in the normal way: Code: $this->load->library('twitter'); This returned an error: "Unable to load the requested class: Twitter" After much experimenting, I found the solution: I moved the Twitter.php file out of the application/libraries folder, and into the system/libraries folder, so that I now had: codeigniter/system/libraries/Twitter.php codeigniter/application/libraries/MY_Twitter.php It now works just fine. I wonder if the documentation might make more explicit the following point: "3rd-party" library classes should be saved in the system/libraries folder. (Extensions to 3rd-party library classes, using the MY_ prefix, should be saved in the application/libraries folder. PS This post is about library classes in general, not specifically about this Twitter class. But, for what it's worth, my new class looked like this: Code: <? [Solution] Extending libraries: proper locations for the Classname and MY_Classname library files - El Forum - 11-04-2009 [eluser]pistolPete[/eluser] Extending a class using the MY_ prefix is reserved to core classes only: http://ellislab.com/codeigniter/user-guide/general/core_classes.html If you need to extend a library which resides in ./application/libraries/ you either need to autoload the base class or use standard PHP require(). [Solution] Extending libraries: proper locations for the Classname and MY_Classname library files - El Forum - 11-04-2009 [eluser]aidehua[/eluser] Thanks for clarifying. Are you saying you advise AGAINST putting a 3rd-party library (like, in my example, Simon Maddox's Twitter.php) into ./system/libraries/? Or, if taking the require() route, do you mean requiring the Someclass.php file from within the MY_Someclass.php file, like this? Code: <?php Then presumably in the controller you'd have to load the MY_ class rather than the original: Code: $this->load->library('MY_Someclass'); |