In order to facilitate the specification of their own code, reference to CodeIgniter, Laravel, PSR and other relevant specifications, to do a summary of the PHP code specification.
[TOC] # # file format
PHP code files must be encoded with UTF-8 without a BOM.
All PHP files must use the Unix LF (linefeed) as the line terminator.
PHP end Tag
All PHP files must end with a blank line.
The pure PHP code file must omit the last?> end tag.
Name of the file
The name of the class file must begin with an uppercase letter, and the names of other files (profiles, views, generic script files, etc.) are all lowercase. In addition, the name of the class file must be consistent with the name of the class.
Naming of namespaces and classes
- CI Framework: The class name must begin with an uppercase letter, use an underscore between multiple words, and do not use the Hump naming method.
- RSP-1: The name of the class must follow the hump naming specification at the beginning of studlycaps capitalization.
Name of the method
- CI Framework: The method of the class should use all lowercase, and should clearly indicate the function of the method, preferably including a verb. Avoid using lengthy names, and use underscores to split between multiple words.
- RSP-1: The method name must conform to the CamelCase ()-style lowercase start hump naming specification.
Name of the property
- CI Framework: The naming conventions of variables are very close to the naming conventions of class methods, use all lowercase, use underscores to split, and should explicitly indicate the purpose of the variable. A very short meaningless variable should only be used as an iterator in the for loop.
- RSP-1: The class's property naming can be followed by a camel ($StudlyCaps), a lowercase start of the camel ($camelCase) or an underscore ($under _score), this specification does not make mandatory, but regardless of which naming method, Should remain consistent within a certain range. This range can be the entire team, the entire package, the entire class, or the entire method.
Comments
Often, you should write more comments, which not only describes the process and intent of the code to those inexperienced programmers, but also helps you understand it a few months later when you look back at your code. Comments do not have a mandatory format, but we recommend the following form.
DocBlock-style annotations, written in front of classes, methods, and property definitions, can be recognized by the IDE:
/** * Super Class * * @package Package Name * @subpackage subpackage * @category category * @author author Nam E * @link http://example.com */class super_class {
/** * Encodes string for use in XML * * @param string $str Input String * @return string */function xml_en Code ($STR)
/** * Data for class manipulation * * @var array */public $data = Array ();
Single-line comments should be combined with code, and a blank line should be left between chunks of comments and code.
Break up the string by Newlines$parts = Explode ("\ n", $str);//A longer comment that needs to give greater-detail on WH At is//occurring and so can use multiple single-line comments. Try to//Keep the width reasonable, around characters is the easiest to//read. Don ' t hesitate to link to permanent external resources//, may provide greater detail:////Http://example.com/informati on_about_something/in_particular/$parts = $this->foo ($parts);
Constant
All letters in a class's constants must be capitalized, and the words are underlined. Refer to the following code:
All keywords in PHP must be all lowercase.
Constants True, FALSE, and null must all be lowercase.
logical operators
Do not use | | operator, which is not visible on some devices (which may look like a number 11), using the && operator is a bit better than using and, but both are acceptable. In addition, you should add a space before and after the operator.
Comparison of return values and type conversions
Some PHP functions return false on failure, but they may also return a valid value such as "" or 0, which are equal when the loose type is compared and false. So when you use these return values for comparison in conditions, be sure to use strict type comparisons to ensure that the return value is really what you want, not the other values of the loose type.
You should also follow this rigorous approach when checking your own return values and variables, using = = = and!== when necessary.
Spaces in the file
Do not leave empty spaces behind the front and end tags of the PHP start tag.
A class one file
Unless several classes are closely related, each class should use a single file.
Space
- CI Framework: Use tabs (tab) in your code instead of spaces, which may seem like a trivial matter, but using tabs instead of spaces allows developers to read your code to customize indentation in their programs, depending on their preferences. Another benefit is that the file can be a little more compact, which would have been four space characters, and now only a single tab is available.
- PSR-2: The code must be indented using 4 space characters instead of the TAB key.
- Indent using tabs, align with spaces.
Code indentation
Functions and control structures must be enclosed in a Allman style.
String
strings are enclosed in single quotation marks, use double quotation marks when there are variables in the string, and wrap the variables with curly braces. In addition, double quotation marks should be used when there are single quotes in the string, so you do not use escape characters.
' My string ' "My string {$foo}" "Select Foo from bar WHERE baz = ' bag '"