首页 > php教程 > php手册 > 正文

PHPautoload PSR-0标准

php中文网
发布: 2016-06-21 08:49:27
原创
1164人浏览过

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速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号