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

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

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

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

Copy after login

test2.php

Copy after login

test.php

Copy after login

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

Copy after login

demo2.php

Copy after login

demo.php

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

Copy after login

App/Controller/Home/Index.php

Copy after login

index.php

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
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!