2

I am considering using CodeIgniter as the framework for my next web-application. However, I already have a large number of helper classes and data structures defined. I could add them to the application's library but I have to rename a large number of files and classes' name to match the criterion

Can I just directly include files as in working on a normal web application, bypassing load->library()? Or is the library reserved for commonly reused classes?

3 Answers 3

8

Even better, put an __autoload() at the bottom of config.php (a weird place, but trust me it's the best) and have it check in application/classes/ then check application/functions.

/*
| -------------------------------------------------------------------
|  Native Auto-load
| -------------------------------------------------------------------
| 
| Nothing to do with config/autoload.php, this allows PHP autoload to work
| for controller types and some third-party libraries.
|
*/
function __autoload($class)
{
    if(strpos($class, 'CI_') !== 0)
    {
        
        $lib = APPPATH . 'libraries/'. $class . EXT;
        $class = APPPATH . 'classes/'. $class . EXT;            
    
        if(file_exists($class))
        {
            include_once $class;
        }
        else if(file_exists($lib))
        {
            include_once $lib;
        }
    }
}

That way you don't need to worry how things are loaded. Use the core libraries as expected and use your classes without including them at all. You can even start moving your classes over to be libraries without needing to worry too much about naming conventions or the loader, and libs can be static, singleton, whatever using this method.

Sign up to request clarification or add additional context in comments.

2 Comments

Phil, first off thanks for the autoload idea. Second, please fix the code above when you get a chance, unless i am wrong, the $class is being reused and hence $lib would be concatenated with $class from the line above and hence $lib would be the wrong location. Hope this makes sense, thanks once again.
Nope, there is nothing wrong with that code. You get the idea anyway, I could write that same autoload 100 times in 100 different ways and have it do the same thing.
2

You can directly include them if you wish. The loading functionality makes things easier, but nothing is stopping you from doing a standard include in php. I've done it several times with third-party classes.

Kohana (a fork of CodeIgniter) is worth checking out if you are still looking for a good PHP framework. In my opinion, it is much better than CodeIgniter.

Comments

1

You don't need to use CodeIgniter's class loaders, you can also require them as you would in any web app, or wrap them in CI-loadable wrappers. In my own projects, I have a few small stand-alone libraries I require from my base controller, and I wrap a few public libraries (like Markdown) for use as a standard CI library.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.