CodeIgniter Forums
Loading own classes with interfaces - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived General Discussion (
+--- Thread: Loading own classes with interfaces (/thread-25847.html)

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

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...


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

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

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:

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