Welcome Guest, Not a member yet? Register   Sign In
Loading libraries in controller vs. loading in CI instance
#2

[eluser]WanWizard[/eluser]
No, the loader refuses to load if the object already exists.

This is indeed quite confusing, and a consequence of the (still present) support for PHP4.
To make sure CI objects are available the are assigned to the class (controller, library) when it loads. Every thing you load after that is visible within the scope you load it, but not outside.

So:
Code:
class MY_Controller
{
    ...
    $this->load->library('MY_LibraryA');
    $this->load->library('MY_LibraryB');
    $this->load->library('MY_LibraryC');
    ...
    $this->My_LibraryB->test();
}

class MY_LibraryB
{
    ...
    function test()
    {
        $this>MY_LibraryA->method(); // works
        $this>MY_LibraryC->method(); // doesn't work
    }
    ....
}

In PHP5, you can overload the __get() method to check if the requested class property is defined, and if not, if the CI object has a property by the requested name. If so, assign it to $this and turn it.

That would mean all objects known to the CI object at any given time are immediately available in all classes, they are dynamically created as soon as they are referenced.

You could even go one step further, and dynamically load the class when the CI object does not exist but the class file does. But that requires a change to the loader class and a decent way of determining what a library is and what a model (or other class). i.e. the issue of namespace.


Messages In This Thread
Loading libraries in controller vs. loading in CI instance - by El Forum - 07-20-2010, 10:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB