PHPautoload PSR-0标准
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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; } |
强制约定
一个合格的名空间-类应当遵循这样的结构
A fully-qualified namespace and class must have the following structure
每个名空间需要有一个*名空间 (“Vendor Name”)(提供者名称).
Each namespace must have a top-level namespace (“Vendor Name”).
每个名空间可以有任意多个子名空间
Each namespace can have as many sub-namespaces as it wishes.
从文件系统载入时,每个名空间分隔符将被转换为一个路径分隔符
Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system.
类名中的每个下划线符( _ )将被转化为一个路径分隔符,名空间中的下划线符( _ )没有任何特定含义
Each _ character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The _ character has no special meaning in the namespace.
一个合格的名空间-类所对应加载的文件必须是以.php结尾的
The fully-qualified namespace and class is suffixed with .php when loading from the file system.
Vendor Name(提供者名称)、名空间、类名中的字符可以是大小写的任意组合
Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case.