This standard contains standard coding elements that are used to ensure interoperability of PHP shared code.
The key words "must must", "must not must not", "REQUIRED required", "shall will", "shall not will not", "should should", "should not should not", "Recomme NDED recommended "," may can ", and" OPTIONAL optional "in this document is to is interpreted as described in RFC 2119.
Overview
- File must use <? PHP and <? = label.
- The PHP code in the file must be UTF-8 encoded with no BOM header.
- A file can declare "symbols" (such as classes, functions, constants, and so on), or it can do something marginal (such as generating output, changing ini settings, etc.). But these two kinds of things should not appear in the same file.
- Namespaces and classes must follow the "autoloading" PSR: [PSR-0, PSR-4].
- The class name must be declared using the StudlyCaps (first-capitalized hump).
- Class constants must use an underscore to split the uppercase letters.
- The method name must use the hump declaration.
File PHP Tags
PHP code must use long label <? PHP?> or Echo short tags <? = ? >; Other forms of labels must not be used.
Character encoding
PHP code must use no BOM header UTF-8 encoding
Marginal effect
A file (which should) either declares symbols (classes, functions, constants, etc.), does not cause any marginal effects, or executes code that produces marginal effects. These two cases cannot appear in a file.
The marginal effect executes logic unrelated to declaring classes, functions, and constants.
"Marginal effects" include, but are not limited to: generating output, explicit require or include, connecting external services, modifying INI configuration, issuing errors or exceptions, modifying global or static variables, reading and writing files, and so on.
The following is an example of declaring a symbol and producing marginal effects; This is the situation that should be avoided:
<?php// 边际效应: 修改 ini 设置ini_set(‘error_reporting‘, E_ALL);// 边际效应: 加载文件include"file.php";// 边际效应: 生成输出echo";// 声明function foo(){ // function body}
The following examples contain only declarations, no marginal effects;
<?php// declarationfunction foo(){ // function body}// 条件式声明 不是边际效应if (! function_exists(‘bar‘)) { function bar() { // function body }}
namespace name and class name
The namespace name and class name must follow the "autoloading" PSR: [PSR-0, PSR-4].
This means that each file can contain only one class and at least one level of namespace: The top-level vendor name.
The class name must be declared with the StudlyCaps (First Capital Hump).
For PHP 5.3 and later versions of the code, you must use a formal namespace (5.3 before PHP does not support namespace).
For example:
<?php//PHP 5.3 and later:namespace Vendor\Model; class Foo{}code written for 5.2. x andBefore should Use the Pseudo-namespacing Convention of vendor_prefixes on class names. <?PHP//PHP5.2.x and earlier:class Vendor_model_foo{}
Class constants, properties, and methods
The "classes" here include classes, interfaces and traits.
4.1. Constants
Class constants must use an underscore-split capitalization declaration.
<?phpnamespaceVendor\Model;class Foo{ const‘1.0‘; const‘2012-06-01‘;}
Property
This guide deliberately avoids bias S T ud l yC aPs, CamelCase and $under _score any one of the attribute naming styles.
Whichever naming style is used, it should be changed to be consistent within a reasonable range. The range can be vendor-level, Package-level, Class-level, or Method-level.
Method
The method name must use Hump CamelCase ().
Php-fig PSR-1 # Basic Coding Standard