PSR-1 basic code specification, psr-1 code specification
This article provides standards for the basic elements of the Code to ensure that the shared PHP code has a high degree of technical interconnectivity.
Key words: "MUST" ("MUST"), "must not/must not" ("must not"), and "need" ("REQUIRED "), "will" ("SHALL"), "no" ("shall not"), "SHOULD" ("shocould"), and "shouldn't" ("shocould NOT ") for detailed descriptions of "Recommendations" ("RECOMMENDED"), "yes" ("MAY"), and "OPTIONAL" ("OPTIONAL"), see RFC 2119.
The PHP code file must start<?php
Or<?=
Label Start;
The PHP code file must startUTF-8 without BOM
Encoding;
In PHP code, only declarations such as classes, functions, and constants should be defined.Subordination
(For example, generating file output and modifying the. ini configuration file), either of them can be selected;
The namespace and classes must comply with the automatic loading specification of the DPC: PSR-4;
The class name must followStudlyCaps
Upper-case hump naming rules;
All the letters of a constant in the class must be uppercase, and words must be separated by underscores;
The method name must complycamelCase
Type.
- File
2.1. PHP tag
PHP code is required<?php ?>
Long tag or<?= ?>
Short output tags; other custom tags are not allowed.
2.2. character encoding
PHP code is required and only usableUTF-8 without BOM
Encoding.
2.3. subordination (side effects)
In a PHP file, you should define only new statements, such as operations that do not produce a subordinate effect, such as classes, functions, or constants, or only logical operations that produce a subordinate effect, but it should not have both.
The word "side effects" means logical operations performed only by including files, without directly declaring classes, functions, and constants.
The "subordinate effect" includes but is not limited to: generating output and directrequire
Orinclude
, Connect to external services, modify ini configurations, throw errors or exceptions, modify global or static variables, read or write files, etc.
The following is an example of an error that contains the Declaration and code that produces the subordination effect:
<? Php // subordination: Modify ini configuration ini_set ('error _ report', E_ALL); // subordination: Introduce file include "file. php "; // subordinate effect: generate output echo"
The following is an example of a code that only contains declarations that do not produce a subordination effect:
<? Php // declare function foo () {// function subject part} // condition declaration ** not ** is a subordinate effect if (! Function_exists ('bar') {function bar () {// function subject }}
- Namespaces and Classes
The namespace and the class name must follow the PSR-4.
According to the specification, each class is independent of a file, and the namespace must have at least one level: top-level organization name (vendor name ).
The class name must followStudlyCaps
Description: Description of the upper case.
PHP 5.3 and later versions of code must use a formal namespace.
For example:
<? Php // namespace Vendor \ Model; class Foo {} for PHP 5.3 and later versions {}
5.2.x and earlier versions should use the pseudo-namespace format. It is customary to use the top-level organization name (vendor name), as shown inVendor_
Is the class prefix.
<? Php // 5.2.x and earlier versions written in class Vendor_Model_Foo {}
- Class constants, attributes, and methods
The "class" here refers to all classes, interfaces, and reusable code blocks (traits)
4.1. Constant
All the letters in the constants of the class must be uppercase and separated by dashes. Refer to the following code:
<?phpnamespace Vendor\Model;class Foo{ const VERSION = '1.0'; const DATE_APPROVED = '2012-06-01';}
4.2. Attributes
Class Attribute names can follow the upper-case hump ($StudlyCaps
($camelCase
) Or an underline separator ($under_score
), This specification is not mandatory, but no matter which naming method is followed, it should be consistent within a certain range. This scope can be the entire team, the entire package, the entire class, or the entire method.
4.3. Method
The method name must complycamelCase()
Type.