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:
\ n ";//Declaring function foo () { //function Body}
The following examples contain only declarations, no marginal effects;
namespace name and class nameThe 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:
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.
Property
This guide deliberately avoids the preference for studlycaps, 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 ().