


PHP expert shares: PHP code writing specifications, summarized very comprehensively
这篇文章主要介绍了关于php大牛分享:php代码编写的规范,总结的很全,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
一直以来,php都是Web开发中使用最频繁的编程语言,也正因为如此,众多的从业者,导致了很多不规范的代码。
PHP-FIG(PHP框架接口组织)制定了一整套完善的标准,推荐给广大的php开发使用。
一共制定了五套标准:
(PSR: PHP推荐标准)
PSR-1: 基本的代码风格;
PSR-2: 严格的代码风格;
PSR-3: 日志记录器接口;
PSR-4: 自动加载
其实还有一个PSR-0,不过已被PSR-4代替了,所以不存在 PSR-0版本。
一、PSR-1:基本代码风格
1. 标签: 必须要把php代码写在 , 或者 = 和 ?>标签中,不得使用其它格式的标签;
2. 编码: 必须采用无BOM头的UTF-8字符集,目前大多数的IDE编辑器都自动支持;
3. 类名: 必须采用驼峰式: CamelCase,这种格式也叫标题式,例如: IndexController;
4. 常量: 必须采用大写字母,多个单词之间采用下划线连接: APP_PATH;
5. 方法: 必须采用小驼峰式: camelCase(),例如: getStatus();
二、PSR-2: 严格的代码风格
1. 代码必须首先符合PSR-1的规范;
2. 缩进: 必须统一采用四个空格;
3. 换行: 必须使用UNIX换行风格;
4. 结尾: 必须要有一个空行,并且不允许有关闭标签 ?>;
5. 每行代码不超过80字符,最多不能超过120个字符;
6. 关键字全部使用小写字母,例如: true,false,use....;
7. 命名空间:后面必须紧跟一个空行;
8. use导入空间后,也必须紧跟一个空行;
9. 类的起始括号{, 必行另起一行;
10. 方法与函数的起始括号{,也必须另起一行;
11. 类中所有成员,必须声明可见性:public, protected,private;
12. 类中成员的特征: abstract, final, 必须放在可见性声明之前;
13. static 关键字,必须放在类成中的可见性声明之后;
14. 控制结构的起始括号必须与语句在同一行,例如: if () {};
15. 控制结构的参数之间,逗号之后必须要有空格,例如:($m, $n);
三、PSR-3: 日志记录接口
这个规范与前面的规范不同,它不是一个推荐标准,而是一个接口标准,规则了日志记录器可以实现的方法。
只要遵循这个标准,就必须实现以下9个方法:
<?php namespace Psr\Log; interface LoggerInterface { public function emergency($message, array $context=[]); public function alert($message, array $context=[]); public function critical($message, array $context=[]); public function error($message, array $context=[]); public function warning($message, array $context=[]); public function notice($message, array $context=[]); public function info($message, array $context=[]); public function debug($message, array $context=[]); public function log($level, $message, array $context=[]); }
四、PSR-4: 自动加载器
1. 为什么要有自动加载器?
之前一个php脚本中,可能会加载大量的文件:
<?php include 'demo1.php'; include 'demo2.php'; include 'demo3.php'; ......
有了自动加载器,就可以根据功能,按需加载。
在没有该标准之前, 我们可以通过__autoload()和spl_autoload_register()进行加载器注册,现在可以借助命名空间实现自动加载。
2. 自动加载原理
主要是将类,接口,trait等所在文件路径,与代码的命名空间进行映射,使之一一对应,赋予了命名空间第二次生命。
例如:
<?php namespace app\controller; class UserController { //代码 }
说明:
1. 类名: app\controller\UserController;
2. 类文件与类同名: app/controller/UserController.php
3. 类名与类文件名,通过命名空间进行映射:
<?php define('ROOT_PATH', __DIR__); spl_autoload_register(function($className){ require ROOT_PATH . '/' . str_replace('\\','/', $className) . '.php'; });
4. 将类名与命名空间进行关联,是现代php开发框架的基础,composer也是基于此实现了组件自动加载;
更多的编程规范,可以登录php中文网(www.php.cn)查阅。
相关推荐:
The above is the detailed content of PHP expert shares: PHP code writing specifications, summarized very comprehensively. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
