Yes, the Codeigniter loader currently follows the singleton pattern, or at least the pattern that most accurately describes it. When you do the following:
$this->load->library('foo'); $this->load->model('foo'); $this->load->database('foo');
The loader performs the following actions:
Check if a previously loaded class is loaded by checking the registry of loaded classes. You can ignore a request with a debug log entry if it has been loaded.
Create an instance of the class with any parameters that you set, create a link to this object in the supermodule (singleton-ish), named after the class, or any ordinary name that you pass. Keep the link, ignore subsequent download attempts.
In bootstrap, magic functions in the global area behind bootloader methods are used to create databases, main libraries, etc.
A more traditional singleton approach could do something like this (with automatic loading):
return $className::instance();
... If the instance method returns an instance or constructs, if it has not yet been created, thus eliminating any need to keep track of what was or was not loaded. If the class has been loaded, a link will be passed, otherwise a new object will be created and returned.
I believe that technically, CI is its own model in this regard, but close enough to the single that this term really applies. This is really a singleton, just not implemented in the usual way.
Finally, I checked that for CI-3 there are patches that make the bootloader more flexible, which allows you to work outside the super-object as you see fit, returning the object or link in these cases, but I donβt know the status of the Ellis lab. taking them.
Tim post
source share