PSR-1 基本代码规范
基本代码规范
本篇规范制定了代码基本元素的相关标准,以确保共享的PHP代码间具有较高程度的技术互通性。
1. 概览 -----------
-
PHP代码文件 必须以
-
PHP代码文件 必须以 不带BOM的 UTF-8编码;
-
PHP代码中 应该只定义类、函数、常量等声明,或其他会产生 从属效应的操作(如:生成文件输出以及修改.ini配置文件等),二者只能选其一;
-
命名空间以及类 必须符合 PSR 的自动加载规范:PSR-0 或PSR-4 中的一个;
-
类的命名 必须遵循 StudlyCaps大写开头的驼峰命名规范;
-
类中的常量所有字母都 必须大写,单词间用下划线分隔;
-
方法名称 必须符合 camelCase式的小写开头驼峰命名规范。
- 文件
2.1. PHP标签
PHP代码 必须使用 长标签 或 = ?>短输出标签;
一定不可使用其它自定义标签。
2.2. 字符编码
PHP代码 必须且只可使用 不带BOM的UTF-8编码。
2.3. 从属效应(副作用)
一份PHP文件中 应该要不就只定义新的声明,如类、函数或常量等不产生从属效应的操作,要不就只有会产生从属效应的逻辑操作,但 不该同时具有两者。
“从属效应”(side effects)一词的意思是,仅仅通过包含文件,不直接声明类、函数和常量等,而执行的逻辑操作。
“从属效应”包含却不仅限于:生成输出、直接的 require或 include、连接外部服务、修改 ini 配置、抛出错误或异常、修改全局或静态变量、读或写文件等。
以下是一个反例,一份包含声明以及产生从属效应的代码:
<?php// 从属效应:修改 ini 配置ini_set('error_reporting', E_ALL);// 从属效应:引入文件include "file.php";// 从属效应:生成输出echo "<html>\n";// 声明函数function foo(){ // 函数主体部分}
下面是一个范例,一份只包含声明不产生从属效应的代码:
<?php// 声明函数function foo(){ // 函数主体部分}// 条件声明**不**属于从属效应if (! function_exists('bar')) { function bar() { // 函数主体部分 }}
- 命名空间和类
命名空间以及类的命名必须遵循 [PSR-0][].
根据规范,每个类都独立为一个文件,且命名空间至少有一个层次:顶级的组织名称(vendor name)。
类的命名必须 遵循 StudlyCaps大写开头的驼峰命名规范。
PHP 5.3及以后版本的代码 必须使用正式的命名空间。
例如:
<?php// PHP 5.3及以后版本的写法namespace Vendor\Model;class Foo{}
5.2.x及之前的版本 应该使用伪命名空间的写法,约定俗成使用顶级的组织名称(vendor name)如 Vendor_为类前缀。
<?php// 5.2.x及之前版本的写法class Vendor_Model_Foo{}
- 类的常量、属性和方法
此处的“类”指代所有的类、接口以及可复用代码块(traits)
4.1. 常量
类的常量中所有字母都 必须大写,词间以下划线分隔。
参照以下代码:
<?phpnamespace Vendor\Model;class Foo{ const VERSION = '1.0'; const DATE_APPROVED = '2012-06-01';}
4.2. 属性
类的属性命名可以遵循 大写开头的驼峰式 ( $StudlyCaps)、小写开头的驼峰式 ( $camelCase) 又或者是 下划线分隔式 ( $under_score),本规范不做强制要求,但无论遵循哪种命名方式,都 应该在一定的范围内保持一致。这个范围可以是整个团队、整个包、整个类或整个方法。
4.3. 方法
方法名称 必须符合 camelCase()式的小写开头驼峰命名规范。

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











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

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.

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

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.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
