


PHP PSR-1 basic code specification (Chinese version), _PHP tutorial
PHP PSR-1 Basic Code Specification (Chinese version),
Basic Code Specification
This specification establishes relevant standards for basic elements of code,
To ensure a high degree of technical interoperability between shared PHP codes.
Keywords "MUST", "MUST NOT", "REQUIRED",
"will" ("SHALL"), "won't" ("SHALL NOT"), "should" ("SHOULD"), "should not" ("SHOULD NOT"),
Detailed descriptions of "RECOMMENDED", "MAY" and "OPTIONAL" can be found in RFC 2119.
1. Overview
-
PHP code file must start with
<?php
or<?=
tag; -
PHP code files must be encoded in
不带BOM的 UTF-8
; -
The in PHP code should only define declarations such as classes, functions, constants, etc., or other operations that will generate
从属效应
(such as generating file output and modifying .ini configuration files, etc.), both of which only You can choose one; -
Namespaces and classes must conform to the PSR autoloading specification: one of PSR-0 or PSR-4;
-
The naming of the class must follow the camel case naming convention starting with
StudlyCaps
capital; -
All letters of the constants in the class must be capitalized, and words should be separated by underscores;
-
The method name must conform to the naming convention of
camelCase
starting with lowercase camel case.
2. File
2.1. PHP tags
PHP code must use the <?php ?>
long tag or <?= ?>
short output tag;
must not use other custom tags.
2.2. Character encoding
PHP code must be and only 不带BOM的UTF-8
encoded.
2.3. Subordinate effects (side effects)
A PHP file should either only define new declarations, such as classes, functions or constants, which do not produce dependency effects, or only logical operations that produce dependency effects, But shouldn’t have both at the same time.
The term "side effects" means that only by including the file without directly declaring the class,
Functions and constants, etc., while performing logical operations.
"Subordinate effects" include but are not limited to: generating output, direct require
or include
, connecting to external services, modifying ini configuration, throwing errors or exceptions, modifying global or static variables, reading or writing files, etc. .
The following is a counter-example, a code containing a declaration and a dependent effect:
<code class="php"><span class="hljs-preprocessor"><?php <span class="hljs-comment">// 从属效应:修改 ini 配置 ini_set(<span class="hljs-string">'error_reporting', E_ALL); <span class="hljs-comment">// 从属效应:引入文件 <span class="hljs-keyword">include <span class="hljs-string">"file.php"; <span class="hljs-comment">// 从属效应:生成输出 <span class="hljs-keyword">echo <span class="hljs-string">"<html>\n"; <span class="hljs-comment">// 声明函数 <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">foo<span class="hljs-params">() { <span class="hljs-comment">// 函数主体部分 } </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
The following is an example of a code that only contains declarations that do not produce dependency effects:
<code class="php"><span class="hljs-preprocessor"><?php <span class="hljs-comment">// 声明函数 <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">foo<span class="hljs-params">() { <span class="hljs-comment">// 函数主体部分 } <span class="hljs-comment">// 条件声明**不**属于从属效应 <span class="hljs-keyword">if (! function_exists(<span class="hljs-string">'bar')) { <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">bar<span class="hljs-params">() { <span class="hljs-comment">// 函数主体部分 } } </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
3. Namespaces and classes
Namespace and class naming must follow PSR-0.
According to the specification, each class is an independent file, and the namespace has at least one level: the top-level organization name (vendor name).
The naming of classes must follow the camel case naming convention of StudlyCaps
starting with an uppercase letter.
Code for PHP 5.3 and later versions must use the official namespace.
For example:
<code class="php"><span class="hljs-preprocessor"><?php <span class="hljs-comment">// PHP 5.3及以后版本的写法 <span class="hljs-keyword">namespace <span class="hljs-title">Vendor\<span class="hljs-title">Model; <span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">Foo { } </span></span></span></span></span></span></span></span></code>
Versions 5.2.x and earlier should use pseudo-namespace writing. It is a convention to use the top-level organization name (vendor name) such as Vendor_
as the class prefix.
<code class="php"><span class="hljs-preprocessor"><?php <span class="hljs-comment">// 5.2.x及之前版本的写法 <span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">Vendor_Model_Foo { } </span></span></span></span></span></code>
4. Class constants, properties and methods
The "class" here refers to all classes, interfaces and reusable code blocks (traits)
4.1. Constants
All letters in the constants of the class must be capitalized, and words are separated by underscores.
Refer to the following code:
<code class="php"><span class="hljs-preprocessor"><?php <span class="hljs-keyword">namespace <span class="hljs-title">Vendor\<span class="hljs-title">Model; <span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">Foo { <span class="hljs-keyword">const VERSION = <span class="hljs-string">'1.0'; <span class="hljs-keyword">const DATE_APPROVED = <span class="hljs-string">'2012-06-01'; } </span></span></span></span></span></span></span></span></span></span></span></code>
4.2. Properties
The attribute naming of the class can follow the camel case format starting with an uppercase ($StudlyCaps
), the camel case format starting with a lowercase case ($camelCase
), or the underscore-delimited format ($under_score
). This specification does not make it mandatory, but No matter which naming scheme is followed, it should be consistent within a certain scope. This scope can be the entire team, the entire package, the entire class, or the entire method.
The method name
must conform to the naming convention of starting with lowercase camel case. camelCase()
因psr-0已废弃故没有转

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.

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 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 and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

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.

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.
