首頁 > php教程 > php手册 > 主體

PHPautoload PSR-0标准

WBOY
發布: 2016-06-21 08:49:27
原創
1035 人瀏覽過

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.



相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!