


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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment