note 100378 deleted from function.spl-autoload-register by danbrown
| From: | danbrown@php.net | Date: | Tue, 12 Oct 2010 16:10:16 +0000 |
| Subject: | note 100378 deleted from function.spl-autoload-register by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173162@lists.php.net to get a copy of this message | ||
Note Submitter: Dormilich at netscape dot net
----
in reply to: a dot schaffhirt at sedna-soft dot de
Autoloading namespaced classes without an explicit autoload function only works on windows (the
namespaceâs backslashes serve as directory separator). however any *nix file system requires
the \ to / conversion.
The PHP Standards Working Group has given an example, which should work in every environment:
<?php
function autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($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;
}
?>