CodeIgniter Forums
Loading own classes with interfaces - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Loading own classes with interfaces (/thread-25847.html)



Loading own classes with interfaces - El Forum - 12-28-2009

[eluser]sl3dg3hamm3r[/eluser]
Hey there

I'm moving away from dealing with associative arrays without any behaviours. This results in writing proper classes. I'm now a bit stucked with loading and instantiating them. I'm aware that there is codeigniter's load_class - function. But this will fail if my class should implement an interface, and furthermore only works when the class resides in the library-folder.

As suggested here, I could implement the __autoload - interceptor into the config-file. This does it actually, but I wanted to figure out if there might be some other ways?

Where do you keep actually your classes which are rather data-providers than functional libraries? E.g. an order-class...

sl3dg3


Loading own classes with interfaces - El Forum - 12-29-2009

[eluser]sl3dg3hamm3r[/eluser]
Hmmm that's a more tricky thing than I would have expected Sad Now I have the following autoloader in my config-file:

Code:
function __autoload($class) {
    //echo "__autoload: loading .$class<br>";
    //prevent CI and Pear classes from being loaded.
    if(!strstr($class, 'CI') && !strstr($class, 'MY_') && !stristr($class, 'PEAR')){
        require "$class.php";
    }
}

This fails as soon as CI tries to load e.g. its own Model-Class:
Message: require(Model.php) [function.require]: failed to open stream: No such file or directory

I don't know how I could prevent the autoloader from trying to load these classes?!?

Of course, another way would be to simply include my class in the controller's constructor where I would need it:
Code:
require_once(BASEPATH."application/models/OrderItemConfig.php");

But as a matter of fact, this won't work for me since I want to serialize the objects to my session and also deserialize it - which means the class needs to be included BEFORE the session reads and deserializes the data. And to include the above line into the config-file is also not a nice solution, I won't need the class everywhere and anytime.... When it comes to performance, this would be a bad solution.

Any other interesting, compelling ideas? Smile