Detailed explanation of PHP object-oriented PSR-0 specification

小云云
Release: 2023-03-20 22:02:01
Original
2183 people have browsed it

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;;
    }
}
Copy after login

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;
    }
}
Copy after login

Object1.php

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

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();
Copy after login

结果

我是Object我是控制器
Copy after login

相关推荐:

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template