All controllers in the OpenCart ecommerce package inherit from the Controller class.
The load variable in those controllers was a bit of a mystery to me, because I could not figure out where it was assigned. Then I realized it was being accessed through the Registry class using a PHP language construct called a “magic method”, which is documented in the online PHP Manual.
In particular the __get magic method is used. This method is quite interesting. It is called whenever a class method attempts to retrieve an undefined attribute.
In a nutshell, since the load variable is not defined in the Controller class or any of its child classes, the __get method will be called.
Let’s see exactly how this works. Below is the __get method as it is defined in the Controller class:
1 2 3 | public function __get($key) { return Registry::get($key); } |
Here is the code that attempts to access the load variable:
1 | $this->load->model('catalog/category'); |
When PHP encounters the above code, to get the $this->load variable, it calls __get with the string “load”, and the implementation of __get looks it up in the Registry class, where it has been previously stored.
Here’s the final piece of the puzzle. You may be wondering how the value of the Registry variable “load” is set.
Simple — at initialization, in the index.php file, a loader object is created and stored in the registry, as follows:
1 2 | $loader = new Loader(); Registry::set('load', $loader); |
© 2009, Sam Mela. All rights reserved.
No Comments so far ↓
There are no comments yet...Kick things off by filling out the form below.