CodeIgniter Forums
load class and not instantiate - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: load class and not instantiate (/thread-41676.html)



load class and not instantiate - El Forum - 05-13-2011

[eluser]uniq[/eluser]
Hello,

How to load class in CI 2 and not instantiate it?

P.S. in CI 1.7+ it was simple by passing second param to load_class function


load class and not instantiate - El Forum - 05-14-2011

[eluser]toopay[/eluser]
As long as i aware of CI upgrade from 1.7.x to 2.x, there is nothing change at how loader class works, related with your issue above.


load class and not instantiate - El Forum - 05-14-2011

[eluser]uniq[/eluser]
FYI

CI 1.7.x system/codeigniter/common.php
Code:
* @access    public
* @param    string    the class name being requested
* @param    bool    optional flag that lets classes get loaded but not instantiated
* @return    object
*/
function &load;_class($class, $instantiate = TRUE)
{
...

CI 2.x system/core/common.php
Code:
* @access    public
* @param    string    the class name being requested
* @param    string    the directory where the class should be found
* @param    string    the class name prefix
* @return    object
*/
function &load;_class($class, $directory = 'libraries', $prefix = 'CI_')
{
...

and the problem is that I have lib with private __construct:
Code:
private function __construct()
{
  //disabled for Singleton pattern
}

of course I can use requered_once but then I have to work with include paths ...


load class and not instantiate - El Forum - 05-14-2011

[eluser]toopay[/eluser]
Thanks for remind me that i missread your thread (what i've looked up is loader class, file under system/core/Loader.php).

Back to your issues, what about extending Common.php?

PS : you can wrap your code, on code tags
Code:
// Remove any spaces
[ code]
// your code here
[ /code]



load class and not instantiate - El Forum - 05-14-2011

[eluser]uniq[/eluser]
thanks for [ code] Smile

I would extend it if there is no way to do it with default CI


load class and not instantiate - El Forum - 01-19-2012

[eluser]ColonelChlorine[/eluser]
Bit of an old thread, but thought I'd throw my 2c in because I ran into the same issue creating my own library which I didn't want CI to interfere with.

I didn't try extending the Common.php file (I don't even think you can because it's just a collection of functions) ... however I wrote my own loader. Threw it in as a helper. It basically accepts a class name, looks for it in the APPPATH."classes" folder and does a require_once().

That's not far off how CI does it's loader, except it does all the singleton + instantiation crap as well.

Works like a charm for me.