CodeIgniter Forums
What are the best practices for loading a "library" without creating an object ("include?") - 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: What are the best practices for loading a "library" without creating an object ("include?") (/showthread.php?tid=21824)



What are the best practices for loading a "library" without creating an object ("include?") - El Forum - 08-21-2009

[eluser]kirkryyn[/eluser]
While I appreciate the extra work CI goes through in order to attach a new instance of a library to the $CI object, sometimes I'd like to just create a one-time use instance or sometimes I may need several unique instances of the same class.

Is there a "lite" equivalent of load->library() that more or less acts just like include()? Or something that works like load->library() that returns an instance instead of attaching it to the $CI object?

I saw this thread which sounds sorta useful, except that it wastes and instance and pollutes my $CI object if I don't really want to put it there.

Are there some good ways to use require_once? Special path stuff I should take into consideration?

I'm pretty new to CI. All in all, I'm liking it so far. I had been afraid this was going to be another CakePHP experience....

The only thing I am not super excited about is having to think about everything being in the context of living in the $CI object. I'll do my best to play along with that philosophy, but it would help ease my mind if I knew that there is an option if I ever get into a bind. Smile


What are the best practices for loading a "library" without creating an object ("include?") - El Forum - 08-21-2009

[eluser]tomcode[/eluser]
Quote:Is there a “lite” equivalent of load->library() that more or less acts just like include()? Or something that works like load->library() that returns an instance instead of attaching it to the $CI object?

No. But using CI's constant APPPATH You can easily write Your own load methods (CI does not use include_path). Additionally I'd recommend to place Your classes in an extra folder under the application folder to prevent confusion.

You could also extend the loader class with these methods.


What are the best practices for loading a "library" without creating an object ("include?") - El Forum - 08-21-2009

[eluser]gwelter[/eluser]
I did this in my code with a helper since helpers are not instantiated (they're made to be a library of global functions). Put your class file in the application/helpers folder, and then either autoload the helper or call
Code:
$this->load->helper('classfile');
when you need it.


What are the best practices for loading a "library" without creating an object ("include?") - El Forum - 08-21-2009

[eluser]kirkryyn[/eluser]
Thanks for the feedback so far! I think it would be a good idea for me to look into the Loader class anyway, but it sounds like going the helper route might be the simplest.


What are the best practices for loading a "library" without creating an object ("include?") - El Forum - 08-21-2009

[eluser]wiredesignz[/eluser]
Quote:Is there a “lite” equivalent of load->library() that more or less acts just like include()? Or something that works like load->library() that returns an instance instead of attaching it to the $CI object?
Yes there is.

You might try using the core function "load_class()", although you are required to use the CI_ class prefix if you require the class instantiated, it does not require CI_ prefix, and will not instantiate the library with FALSE as the second parameter.

Code:
load_class('Custom_lib', FALSE);

Alternatively you might consider using the plugin convention. Plugins are loaded but not instantiated. Probably more appropriate than as a helper.