| 1234567891011121314 |
<?phpfunction 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;} |
強制約定
一個合格的名空間-類應當遵循這樣的結構 <Vendor Name>(<Namespace>)*<Class Name>
A fully-qualified namespace and class must have the following structure <Vendor Name>(<Namespace>)*<Class Name>
每個名空間需要有一個頂級名空間 (“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.