PHP design pattern one: namespace, automatic loading class, PSR-0 coding specification

不言
Release: 2023-03-23 11:58:01
Original
1739 people have browsed it

本篇文章介绍的内容是php设计模式一之命名空间、自动加载类、PSR-0编码规范 ,现在分享给大家,有需要的朋友可以参考一下

一、命名空间:解决在生产环境中多人协同开发时出现类名或函数名冲突的问题;
test1.php

<?php
namespace Test1;
function test(){
    echo "Test1命名空间下的test()方法";
}
?>
Copy after login

test2.php

<?php
namespace Test2;
function test(){
    echo "Test2命名空间下的test()方法";
}
?>
Copy after login

test.php

<?php
require_once(&#39;test1.php&#39;);
require_once(&#39;test2.php&#39;);
Test1\test();    //调用Test1命名空间下的test()方法
Test2\test();
?>
Copy after login

二、自动加载类:解决在项目中引入过多的依赖类文件问题;
demo1.php

<?php
class Demo1{
    static function test(){
        echo "Demo1类中的test()静态方法";
    }
}
?>
Copy after login

demo2.php

<?php
class Demo2{
    static function test(){
        echo "Demo2类中的test()静态方法";
    }
}
?>
Copy after login

demo.php

<?php
//php5.3+版本,spl_autoload_register()自动注册加载类。参数为某个自定义的函数
spl_autoload_register(&#39;autoload&#39;);
//自定义一个自动加载函数,$class为类名,无须传参。spl_autoload_register()方法自动认别
function autoload($class) {
    require_once(__DIR__.&#39;/&#39;.$class.&#39;.php&#39;);
}
Demo1::test();
Demo2::test();
//php5.3版本以下,使用__autoload()方法
function __autoload($class) {
    require_once(__DIR__.&#39;/&#39;.$class.&#39;.php&#39;);
}
?>
Copy after login

三、PSR-0编码规范
1)、必须使用命名空间并且与文件的绝对路径一致;
2)、类名首字母须大写且与文件名保持一致;
3)、除入口文件以外其他php文件必须只有一个类且没有可执行的代码;

四、基于PSR-0编码规范编写一套基础框架
1)、目录结构

App            存放业务逻辑及功能实现的代码
|--Controller
    |--Home
       |--Index.php
Frame          存放与业务逻辑无关的代码,框架部分
|--Autoloader.php  自动加载类
index.php       单一入口文件
Copy after login

Autoloader.php

<?php
namespace Frame;
class Autoloader
{
    static function autoload($class)
    {
        require_once(BASEDIR.&#39;/&#39;.str_replace(&#39;\\&#39;,&#39;/&#39;,$class).&#39;.php&#39;);
    }
}
?>
Copy after login

App/Controller/Home/Index.php

<?php
namespace App\Controller\Home;

class Index
{
    static function test()
    {
        echo "Home控制器下的test()静态方法";
    }
}
?>
Copy after login

index.php

<?php
define(&#39;BASEDIR&#39;,__DIR__);
require_once(BASEDIR.&#39;/Frame/Autoloader.php&#39;);
spl_autoload_register(&#39;Frame\\Autoloader::autoload&#39;);
App\Controller\Home\Index::test();

//输出结果:Home控制器下的test()静态方法
?>
Copy after login

                                                  

The above is the detailed content of PHP design pattern one: namespace, automatic loading class, PSR-0 coding 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!