Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter Autoload
#1

[eluser]Lazos[/eluser]
When I use the autoloading CodeIgniter creates a new object for the class that is loaded or is using the singleton pattern to avoid creating the same object more than once?
#2

[eluser]Phil Sturgeon[/eluser]
CI will only load each model, library or helper once.
#3

[eluser]Lazos[/eluser]
Thanks!
#4

[eluser]nikefido[/eluser]
So, to be clear -
CI's loader class creates singletons and always passes the one instantiated instance of an object.

So to "break out" of the singleton pattern, I would most likely need to create my own factory class that passes a new object instance when called, correct?
#5

[eluser]Pascal Kriete[/eluser]
Right on the money, nikefido.
#6

[eluser]migsy[/eluser]
Hello

I am having troubles to get a Model class act as a singleton, and I was wondering whether anyone could help me out.

I would like a Model class to store some data fetched from a DB. As this data won't change (it's actually taxonomy data), I would like to load the model class just once with the autoloader (and the model's constructor method will fetch the data from the DB and store it on class variable). Basically a singleton class.

But I can't get it to work.

Code:
class Taxonomy_Model extends Model
{

    public $nodes; // array of nodes

    function Taxonomy_Model()
    {
        
        parent::Model(); // have tried also with parent::__construct();
        
        $this->nodes = new Tree($this); // assign an object to the variable

        $this->populate(); // this method populates the variable nodes with data
      
    }

    function printTaxonomy() {
        // code here to print out the variable
    }

I have the above model class, with a variable called nodes. When I call the function printTaxonomy from different methods on a Controller (method controller/displayTaxonomy1 and method controller/displayTaxonomy2), the model's constructor Taxonomy_Model() gets called every time (I have logs inside the model's constructor), therefore I am not achieving the singleton behaviour that I am after.

I have added the model class to the autoload file (how does CI picks up this new configuration? do I have to restart CI, Apache?).

Hope someone can help me out as I am a bit stuck with this.

Many thanks in advance.

Migsy




Theme © iAndrew 2016 - Forum software by © MyBB