CodeIgniter

CodeIgniter Autoload Hook by Shane Pearson – Modified

Shane Pearson used to have this up on BitBucket, but he’s since deleted it. If you were to get a copy of it from his blog post and try to run it in CodeIgniter 2.1.3 or newer, you’ll find that it stopped working. This is because Shane utilized a constant that has been marked as deprecated for quite some time now that has finally been removed. Anyway, I use this lib, so I’m going to post my updates to it for others. I won’t explain it, as Shane does a very good job of that himself. Just go to his blog post to see it.

// change the enable_hooks setting in ./application/config/config.php to read as so:
$config['enable_hooks'] = true;
// This goes into ./application/config/hooks.php
$hook['pre_system'] = array(
	'class' => 'MY_Autoloader',
	'function' => 'register',
	'filename' => 'MY_Autoloader.php',
	'filepath' => 'hooks',
	'params' => array(APPPATH.'core/')
	// moved to reflect CodeIgniters core file path - mladoux
	//'params' => array(APPPATH.'base/')
);
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * MY_Autoloader Class
 *
 * Modified for CodeIgniter 2.1.3 and newer by Mark LaDoux.
 * All modifications notated by my github handle - mladoux
 * All other code by Shane Pearson.
 * Still compatible with older 2.x versions of CI since the modifications
 * were extremely minor ( only a deprecated constant removed ).
 *
 * @package CodeIgniter
 * @subpackage Hooks
 * @category Hooks
 * @author Shane Pearson <shane@highermedia.com>
 * @author Mark LaDoux <mark.ladoux@gmail.com>
 */
class MY_Autoloader 
{
	private $_include_paths = array();

	/**
	 * Register the autoloader function.
	 *
	 * @access public
	 * @param array include paths
	 * @return void
	 */
	public function register(array $paths = array())
	{
		$this->_include_paths = $paths;
		spl_autoload_register(array($this, 'autoloader'));
	}

	// --------------------------------------------------------------------

	/**
	 * Autoload base classes.
	 *
	 * @access public
	 * @param string class to load
	 * @return void
	 */
	public function autoloader($class)
	{
		foreach($this->_include_paths as $path)
		{
			// EXT is deprecated, removed (mladoux)
			// $filepath = $path . $class . EXT;
			$filepath = $path . $class . '.php';

			if(! class_exists($class, FALSE) AND is_file($filepath))
			{
				include_once($filepath);

				break;
			}
		}

	}

	// --------------------------------------------------------------------

} // end class MY_Autoloader

/* End of file MY_Autoloader.php */
/* Location: ./application/hooks/MY_Autoloader.php */

2 thoughts on “CodeIgniter Autoload Hook by Shane Pearson – Modified”

  1. hi

    i tried to use your solution with the latest dev ci from git
    and no luck the function autoloader is never called

    Like

    1. Open your config.php in application/config/ and change $config[‘enable_hooks’] so that it equals true. I forgot to add that to this article, I’ll make an update.

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.