patternphpModerate
PHP Autoloader Library
Viewed 0 times
phpautoloaderlibrary
Problem
Basically, I had written this class a little while ago to ease autoloading of our local libraries.
The premise is that everything is split by packages into multiple layers of sub-packages. Classes are named using CamelCasing. So, a class' name is related to its package as follows:
```
/**
* A class for lazy-loading other classes
*
* This class enables lazy-loading of php classes. The benefit of this is
* three-fold. First, there is a memory benefit, since not all classes are
* loaded until they are needed. Second, there is a time benefit, since not all
* classes are loaded. Third, it produces cleaner code, since there is no need
* to litter files with require_once() calls.
*
* @category Libraries
* @package Libraries
* @author Me
*/
abstract class Loader
{
/**
* @var array An array of class to path mappings
*/
protected static $classes = array();
/**
* @var boolean Has the loader been initialized already
*/
protected static $initialized = false;
/**
* @var array An array of auto-search paths
*/
protected static $namedPaths = array(
'exception',
'interface',
'iterator',
);
/**
* @var array An array of include paths to search
*/
protected static $paths = array(
PATH_LIBS,
);
/**
* Tell the auto-loader where to find an un-loaded class
*
* This can be used to "register" new classes that are unknown to the
* system. It can also be used to "overload" a class (redefine it
* elsewhere)
*
* @param string $class The class name to overload
* @param string $path The path to the new class
*
* @throws InvalidArgumentException Upon an Invalid path submission
The premise is that everything is split by packages into multiple layers of sub-packages. Classes are named using CamelCasing. So, a class' name is related to its package as follows:
PackageSubpackageSubpackageName. Now, each package can have package specific interfaces defined by isPackageName for interfaces, exceptions by PackageNameException, etc. I tried to make it flexible enough for reuse.```
/**
* A class for lazy-loading other classes
*
* This class enables lazy-loading of php classes. The benefit of this is
* three-fold. First, there is a memory benefit, since not all classes are
* loaded until they are needed. Second, there is a time benefit, since not all
* classes are loaded. Third, it produces cleaner code, since there is no need
* to litter files with require_once() calls.
*
* @category Libraries
* @package Libraries
* @author Me
*/
abstract class Loader
{
/**
* @var array An array of class to path mappings
*/
protected static $classes = array();
/**
* @var boolean Has the loader been initialized already
*/
protected static $initialized = false;
/**
* @var array An array of auto-search paths
*/
protected static $namedPaths = array(
'exception',
'interface',
'iterator',
);
/**
* @var array An array of include paths to search
*/
protected static $paths = array(
PATH_LIBS,
);
/**
* Tell the auto-loader where to find an un-loaded class
*
* This can be used to "register" new classes that are unknown to the
* system. It can also be used to "overload" a class (redefine it
* elsewhere)
*
* @param string $class The class name to overload
* @param string $path The path to the new class
*
* @throws InvalidArgumentException Upon an Invalid path submission
Solution
The first problem I see is that there are a lot of cases where someone will want to create a class with more than one word in the name (DataMapper), and the autoloader you have provided won't allow for that. I would recommend using another character to delimit between package names. The Zend Framework uses Package_SubPackage_SubPackage_Class and that works very well.
As an aside, I'm not sure what your specific reasons are for writing your own autoloader (whether it is for production, education, etc.), but if you are planning to use it for production, I would recommend the Zend_Loader class from the Zend Framework, as it is supported, fully tested, and continually being developed. You can read it's quickstart guide Here
As an aside, I'm not sure what your specific reasons are for writing your own autoloader (whether it is for production, education, etc.), but if you are planning to use it for production, I would recommend the Zend_Loader class from the Zend Framework, as it is supported, fully tested, and continually being developed. You can read it's quickstart guide Here
Context
StackExchange Code Review Q#4, answer score: 14
Revisions (0)
No revisions yet.