1. overview... 21.1. main content of the document... 21.2. target... 22. PHP file format requirements... 22.1. general requirements... 22.2. shrink... 22.3. maximum number of characters in a row... 22.4. line Terminator... 33. naming rules... 33.1. class Name... 33.2. interface Class Name... 43.3. file Name... 43.4. name of functions and methods... 43.5. variable name... 53.6. constant name... 54. encoding style... 64.1. PHP code delimiters... 64.2 string writing style... 64.3. array... 74.4. class... 84.5. functions and methods... 94.6. control statement... 114.7. internal documentation 1. summary 1.1. document Content
This document provides guidance and suggestions on the standardization of php development, including:
L PHP file format requirements
L naming rules
L encoding style Specification
L documented requirements in code
1.2. Objectives
Coding standards are very important for any development project, especially for multi-person collaboration. Good coding standards mean high code quality, less bugs, and easy maintenance.
2. php file format requirements 2.1. General requirements
For files that only contain PHP code, the PHP end tag ("?>") is not allowed "), Although PHP syntax does not require this, removing this end tag can avoid unexpected output of white space characters at the end, resulting in unnecessary impact.
Important:Although _ halt_compiler () can be used in PHP to contain arbitrary binary data, this is prohibited in this specification, this is allowed only when special scripts are installed.
2.2. scale down
4 space characters (KEYS) are used for indentation. Do not use TAB characters (KEYS ).
2.3. Maximum number of characters in a row
Each line should not exceed 80 characters, that is, the code of each line should not exceed the boundary of 80th columns in actual development process. It is also possible to slightly exceed this boundary. the maximum number of characters in each line of PHP code is 120.
2.4. line terminator
The line terminator follows the UNIX text file standard, that is, each line ends with a line break (LF). In the ASCII code, the line break is a decimal value of 10 and the hexadecimal value is 0x0a.
Do not use the Macintosh System Standard: The line terminator is a carriage return (Cr, hexadecimal value is 0x0d.
Do not use Windows Standard: Press enter/line feed (CRLF, 0x0d, 0x0a) as the line terminator.
3. Naming Convention 3.1. Class naming
Zend framework introduces a hierarchical class naming method, which can be directly mapped to the directory where the file is actually stored by using the class name. In this hierarchical structure, the root directory of Zend framework is "Zend/", so class files are saved separately at a certain directory level.
The class name can only contain letters and numbers, but numbers are not recommended. Underlines can only be used to replace path delimiters. For example, the file name "Zend/DB/table. php" will be mapped to the class name "zend_db_table ".
If the class name is composed of multiple words, the first letter of each word must be in upper case and the other letters in lower case. For example, the class name "zend_pdf" is not allowed, it should be named "zend_pdf ".
The class released by Zend (or a partner company involved in Zend framework development) in Zend framework. Its name always starts with "Zend, the corresponding files are stored in the hierarchical directory "Zend.
Example of standardized class naming:
Zend_db
Zend_view
Zend_view_helper
Important:Code that does not belong to the Zend framework in project development. For example, the Code is developed by the Framework user. The class name must not start with "Zend, only the classes published by Zend (or partners involved in Zend framework development) can be named after Zend.
3.2. Interface Class Name
The naming rules of interface classes are similar to those of other classes (see the previous section), but it is best to end with the word "interface", for example:
Zend_log_adapter_interface
Zend_controller_dispatcher_interface
3.3. File Name
Only letters, numbers, underscores, and hyphens (-) are allowed for all files. spaces and other special characters are not allowed.
Any file containing PHP code must start ". PHP "is a file name suffix. Below are some examples of standardized File naming. These examples correspond to the examples of class naming in the previous section.
Zend/DB. php
Zend/controller/front. php
Zend/View/helper/formradio. php
As in the preceding example, the file name must be consistent with the class name.
3.4. Name of functions and methods
Function names can only contain letters and numbers. Underlines are not allowed. Although numbers are allowed, they are not recommended.
The function name must start with a lowercase letter. If the function name contains multiple words, the first letter of all words except the first word is capitalized, which is generally referred to as "studlycaps" or "camelcaps ").
To enhance code readability, the name can be detailed and lengthy to a certain extent.
The following are some examples of standardized function naming:
Filterinput ()
Getelementbyid ()
Widgetfactory ()
For object-oriented programming, the object accessors are always prefixed with "get" or "set". When using design patterns, such as Singleton or factory ), the naming method usually contains the pattern name, which enhances code readability and clearly shows the design pattern used.
This specification does not allow functions with a global scope (that is, functions outside the object). These functions should be packaged into a static class.
3.5. variable naming
Variable names can only contain letters and numbers, and cannot contain underscores (unless in the following cases). Similarly, numbers are allowed but not supported.
For a class member variable declared as private or protected, the variable name must start with an underscore, which is the only condition in the variable naming rule that permits the use of underscores, the member variables declared as public are not allowed to use underscores.
Consistent with the function naming rules (see section 3.3), the variable name must start with a lower-case letter, followed by the upper-case camelcaps ).
To enhance readability, variable naming also requires a certain degree of detail and length, and requires practical significance. Variable names like $ I and $ n are not allowed in other cases except for loop statements with less code. If the loop statement contains more than 20 lines of code, the index variables used for the loop should also use meaningful names.
3.6. Constant naming
A constant name can contain letters and numbers. Unlike function names and variable names, it can also contain underscores (_) and has no restrictions on the use of numbers.
All letters of a constant must be capitalized.
A constant must be defined as a class member using the const indicator. The definition of a constant using define in the global scope is not supported.
4. Encoding style 4.1. PHP code delimiters
The PHP code delimiter must use the complete standard PHP demarcation Tag:
<? PHP
?>
Do not use short tags.
4.2 string Writing Style 4.2.1 pure text string
For plain text strings (without variable substitution), you must use single quotation marks to include:
$ A = 'example string ';
4.2.2. string containing quotation marks
For character strings containing quotation marks, double quotation marks are allowed. This method is mostly used for writing SQL statements:
$ SQL = "select 'id', 'name' from 'people' where 'name' = 'fred 'or 'name' = 'Susan '";
The above writing method is clearer and more readable than using escape characters with quotation marks.
4.2.3. string with Variable Substitution
For strings with variable substitution, either of the following methods is allowed:
$ Greeting = "Hello $ name, welcome back! ";
$ Greeting = "Hello {$ name}, welcome back! ";
To ensure writing consistency, the following methods are not allowed:
$ Greeting = "Hello $ {name}, welcome back! ";
4.2.4. String connection
Multiple strings can be connected using the dot operator ("."), but each dot operator must have a space before and after it to enhance readability.
$ Company = 'zend'. 'source ';
When using the dot operator to connect multiple strings, you can split the statement into multiple rows to enhance readability. In this case, each subsequent row should be filled with spaces, align the vertex operator with the equal sign:
$ SQL = "select 'id', 'name' from 'people '"
. "Where 'name' = 'Susan '"
. "Order by 'name' ASC ";
4.3. array 4.3.1. Numeric Index Array
The index of the array cannot use negative numbers.
Index arrays can start with any non-negative integer, but this is not recommended. We recommend that each array start with index 0.
When the array keyword is used to define the index array, each comma should be followed by a space character to enhance readability:
$ Samplearray = array (1, 2, 3, 'zend', 'Studio ');
The statement that uses the array keyword to define the index array can be split into multiple rows. In this case, each subsequent row should be filled with spaces to keep the line aligned as follows:
$ Samplearray = array (1, 2, 3, 'zend', 'studio ',
$ A, $ B, $ C,
56.44, $ D, 500 );
4.3.2. Associate an array
When using the array keyword to define the associated array, we recommend that you split the statement into multiple rows. In this case, use spaces to align the keys and values of each row:
$ Samplearray = array ('firstkey' => 'firstvalue ',
'Secondkey' => 'secondvalue ');