CodeIgniter’s models may be added to autoload.php (which will always load them even if they’re not required) or must be manually loaded using code such as:
$this->load->model('views\account\logon');
While it’s only one line of code, it soon becomes tedious having to have a similar line across every action in every controller. Consequently, I created a CodeIgniter library (after taking inspiration from Doctrine’s ClassLoader) which uses spl_autoload. Class_loader automates the process of loading classes within a defined namespace and only requires a single line of configuration; therefore no changes to autoload.php or manual calls to load are required.
Class_loader.php:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Class_loader is a CodeIgniter library which uses SPL autoload to
* automatically include classes within the defined namespace.
*
* @author Andrew Mackrodt <andrew@ajmm.org>
* @version 2011.06.23
*/
class Class_loader
{
/**
* @param string $namespace The namespace to apply the autoloader.
* @param string $includePath
* The parent folder of the root namespace. Defaults to CodeIgniter's
* APPPATH if no value is given.
*/
public function register($namespace, $includePath = null)
{
if (!empty($namespace) && $namespace[0] == '\\')
{
// Strip leading namespace separator.
$namespace = substr($namespace, 1);
}
if (empty($includePath))
{
// Set the include path to the CI application folder.
$includePath = APPPATH;
}
// Ensure the include path ends with the OS directory separator.
$includePath = rtrim($includePath, '\\/').DIRECTORY_SEPARATOR;
// Register the auto-loader.
$classLoader = new _ClassLoader($namespace, $includePath);
spl_autoload_register(array($classLoader, 'loadClass'));
}
}
/**
* A helper class to be used by Class_loader.
*/
class _ClassLoader
{
private $namespace;
private $includePath;
public function __construct($namespace = null, $includePath = null)
{
$this->namespace = $namespace;
$this->includePath = $includePath;
}
public function loadClass($className)
{
if ($this->namespace !== null
&& strpos($className, $this->namespace.'\\') !== 0)
{
return false;
}
// Convert a namespace to a path using the OS directory separator.
$classPath = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $className);
require_once $this->includePath.$className.'.php';
return true;
}
}
Usage:
The classes you wish to autoload must contain a namespace and be located appropriately, e.g. I have a Script class which is located at APPPATH/models/views/shared/Script.php and has the namespace models\views\shared.
$this->class_loader->register('models\views', APPPATH);
