CodeIgniter - Using existing classes with CodeIgniter, any problems with including them directly?
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 libr开发者_运维知识库ary reserved for commonly reused classes?
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.
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.
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.
精彩评论