Deprecated - As of 2014-10-21 PSR-0 has been marked as deprecated. PSR-4 is now recommended as an alternative.
The following describes the mandatory requirements that must be adhered to for autoloader interoperability.
\<Vendor Name>\(<Namespace>\)*<Class Name>
DIRECTORY_SEPARATOR
when
loading from the file system._
character in the CLASS NAME is converted to a
DIRECTORY_SEPARATOR
. The _
character has no special meaning in the
namespace..php
when
loading from the file system.\Doctrine\Common\IsolatedClassLoader
=> /path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php
\Symfony\Core\Request
=> /path/to/project/lib/vendor/Symfony/Core/Request.php
\Zend\Acl
=> /path/to/project/lib/vendor/Zend/Acl.php
\Zend\Mail\Message
=> /path/to/project/lib/vendor/Zend/Mail/Message.php
\namespace\package\Class_Name
=> /path/to/project/lib/vendor/namespace/package/Class/Name.php
\namespace\package_name\Class_Name
=> /path/to/project/lib/vendor/namespace/package_name/Class/Name.php
The standards we set here should be the lowest common denominator for painless autoloader interoperability. You can test that you are following these standards by utilizing this sample SplClassLoader implementation which is able to load PHP 5.3 classes.
Below is an example function to simply demonstrate how the above proposed standards are autoloaded.
<?php
function autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $fileName;
}
spl_autoload_register('autoload');
The following gist is a sample SplClassLoader implementation that can load your classes if you follow the autoloader interoperability standards proposed above. It is the current recommended way to load PHP 5.3 classes that follow these standards.