


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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.
