Home >Backend Development >PHP Tutorial >Detailed explanation of PHP object-oriented PSR-0 specification

Detailed explanation of PHP object-oriented PSR-0 specification

小云云
小云云Original
2018-03-08 13:21:002221browse

PSR-0规范1.命名空间必须与绝对路径一致 2.类名首字母必须大写 3.除入口文件外,其它.php必须只有一个类。

开发符合PSR-0规范的基础框架

1.全部使用命名空间
2.所有PHP文件必须自动载入,不能有include/require
3. 单一入口

项目

目录
Detailed explanation of PHP object-oriented PSR-0 specification

控制器中的index.php

<?phpnamespace App\Controller\Home;class Index{
    static function test()
    {
        echo &#39;我是控制器&#39;;
    }
}

Loader.php

<?php/**
 * User: baldy
 * CreateTime: 2018/2/27 下午5:27
 * Description:
 */namespace IMooc;class Loader{
    static function autoload($class)
    {
        $file = BASEDIR.&#39;/&#39;.str_replace(&#39;\\&#39;,&#39;/&#39;,$class).&#39;.php&#39;;        require $file;
    }
}

Object1.php

<?phpnamespace IMooc;class Object1{
    static function test(){
        echo "我是Object";
    }
}

index.php

<?phpdefine(&#39;BASEDIR&#39;,__DIR__);
include BASEDIR . &#39;/IMooc/Loader.php&#39;;
spl_autoload_register(&#39;\\IMooc\\Loader::autoload&#39;);
IMooc\Object1::test();
App\Controller\Home\Index::test();

结果

我是Object我是控制器

相关推荐:

PSR-0 自动加载标准

PHP的PSR-0命名标准

PHP的PSR-0标准利用namespace来做autoloading

The above is the detailed content of Detailed explanation of PHP object-oriented PSR-0 specification. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn