• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can I load a library class without creating an instance of it? [SOLVED]

#1
[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!

#2
[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).

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.