CodeIgniter Forums
Can I load a library class without creating an instance of it? [SOLVED] - 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: Can I load a library class without creating an instance of it? [SOLVED] (/showthread.php?tid=43839)



Can I load a library class without creating an instance of it? [SOLVED] - El Forum - 07-25-2011

[eluser]Loupax[/eluser]
First post! Yay!

Ok, here is the deal: I try to save as much memory as I can in my app, and to do so, I use refferences a lot. In order to count the instances of my objects, all my library classes have these attributes and functions:

Code:
private static $instances=0;

public __construct(){
ClassName::$instances++;
//Rest of constructor
}

public __destuct(){
ClassName::$instances--;
//Rest of destructor
}

The following code shows me that an instance is created at load time

Code:
$this->load->library('ClassName');
echo "Instances of ClassName in memory: ".ClassName::$instances;

//Outputs: Instances of ClassName in memory: 1

Soooo, here are my questions:

1) I know that just one extra instance is no big deal, but it bugs me that it's there... Can I load the class without creating it, or even destroy it somehow?

2) This causes problems when I want to auto load a class that has parameters in it's constructor, as I don't see a way to pass parameters in the Autoload... To resolve i did the following, but I don't like it much...

Code:
<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class ClassName {
    public function  __construct($params=NULL) {
        if (!$params) return;
        //Rest of the constructor...
    }
}
?>

Is there a better way to autoload classes with parameters?

Ok, that's enough for now, thanks for your time!


Can I load a library class without creating an instance of it? [SOLVED] - El Forum - 07-26-2011

[eluser]danmontgomery[/eluser]
1) Not using the loader. You can do this by including/requiring the files like you would outside of CI.
2) Nope. To get around this you can create a MY_Controller and load what you need there (with or without parameters).


Can I load a library class without creating an instance of it? [SOLVED] - El Forum - 07-26-2011

[eluser]Loupax[/eluser]
Heh! I got so excited about CI, i totally forgot I could simply inlude_once the classes I needed! Thanks!