This part of the standard includes what should be considered standard coding elements to ensure a high degree of technical interoperability between shared PHP code.
Keywords "must" ("must"), "must not/must not be" ("must not"), "Need" ("REQUIRED"),
"Will" ("Shall"), "no" ("Shall not"), "should" ("should"), "shouldn't" ("should not"),
Detailed descriptions of "recommended" ("RECOMMENDED"), "Can" ("may") and "optional" ("OPTIONAL") can be found in RFC 2119
1. Overview
- PHP files must be used only
<?php
and <?
these two tags.
- PHP files must be encoded with UTF-8 without a BOM.
- The php file should only define declarations such as classes, functions, constants, or other actions that would have a subordinate effect (such as generating file output and modifying. ini configuration files, etc.), but should not do two things at the same time.
- Namespaces and classes must conform to the automatic loading specification for PSR: PSR-0 or PSR-4;
- The name of the class must follow the Hump naming rules () that begin with uppercase
StudlyCaps
.
- Constants in a class all letters must be capitalized, and the words are separated by underscores.
- The method name must conform to the Hump naming method (), which begins with lowercase
camelCase
.
2. File 2.1PHP Tag
PHP code must use <?php ?>
long or <?= ?>
short label output. You must not use a different label.
2.2 Character encoding
PHP code must only be UFT-8 encoded with no BOM
2.3 Dependency effect (side effects)
A file should either define a declaration (class, function, solid, etc.) or only a logical operation that produces a subordinate effect, but not both.
The phrase "side effects" (side effects) means that the logic is executed only from the containing file, not directly related to declaring classes, functions, constants, etc.
The "dependent 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, and so on.
Here is an example of both declaring and subordination effects, which should be avoided:
<?php// side effect: change ini settingsini_set(‘error_reporting‘, E_ALL);// side effect: loads a fileinclude"file.php";// side effect: generates outputecho";// declarationfunction foo(){ // function body}
Here is an example that contains only declarations that are not included side effects
, and should be imitated:
<?php// declarationfunction foo(){ // function body}// conditional declaration is *not* a side effectif (! function_exists(‘bar‘)) { function bar() { // function body }}
3. Namespaces and Classes
Namespaces Namespace
and Classes class
must follow the " autoloading
" PSR standard: [PSR-0, PSR-4].
This means that each class must be independent of a file that belongs to him, and that the namespace has at least one level: the top-level organization name (vendor name).
The name of the class must follow the Hump naming rules () that begin with uppercase StudlyCaps
.
PHP5.3 and later must use the formal namespace.
For example:
<?php// PHP 5.3 and later:namespaceVendor\Model;class Foo{}
5.2.x and previous versions should use a Vendor_
pseudo-namespace that is prefixed with the class name
<?php// PHP 5.2.x and earlier:class Vendor_Model_Foo{}
4. Constants, properties, and methods of a class
The "class" here refers to all classes, interfaces, and reusable code blocks (traits)
Translator Note: Trait refers to all functions that can be reused in PHP5.4. http://php.net/manual/en/language.oop5.traits.php
4.1 Constants
The constants of a class must be all uppercase and separated by an underscore.
For example:
<?phpnamespaceVendor\Model;class Foo{ const‘1.0‘; const‘2012-06-01‘;}
4.2 Properties
The property naming of a class can follow the camel-like () $StudlyCaps
, lower-case-beginning camel () $camelCase
or underscore-delimited ( $under_score
), but this specification does not impose a mandatory requirement, but it should be consistent within a certain scope regardless of which naming method is followed. This range can be the entire team, the entire package, the entire class, or the entire method.
4.3 methods
The method name must conform to the Hump naming method (), which begins with lowercase camelCase()
.
PSR-1 Basic Code specification