4.4. Declaration of class 4.4.1.
Class declaration should comply with the following requirements:
L braces must be written in the next line of the class name;
L each class must have a comments document block complying with the phpdocumentor standard;
L The code inside the class must be indented by four spaces;
L only one class is allowed for a PHP file;
L other code can be placed in a class file, but it is not recommended. In this case, two blank lines must be used to separate the class code from other PHP code.
The following is a declaration of a standard class:
/**
* Document comment Block
*/
Class sampleclass
{
// Internal code of the class
// Four spaces must be indented.
}
4.4.2. class member variables
Member variables must follow the naming rules of variables.
The declaration of class member variables must be at the top of the class before the function is defined.
The VaR keyword is not allowed. Keywords: private, protected, or public must be used for the declaration of member variables. Although you can declare variables as public to directly access member variables, we recommend that you use GET/set accessors to access variables.
4.5. Functions and Methods 4.5.1. Definitions of functions and methods
The function name must follow the naming rules.
Internal functions of the class must use keywords such as private, protected, and public to indicate the visibility of the function.
The usage of braces in the function is the same as that of the class. That is, braces must be located in the next line of the function name, and there is no space between the function name and the brackets.
We strongly recommend that you do not use global range functions.
The following describes how to write a standard class member function:
/*
* Document comment Block
*/
Function samplemethod ($)
{
// Internal content of the Function
// Four spaces must be indented.
}
Note:You can pass "reference transfer variable" only in the function definition phase ":
Function samplemethod (& $)
{}
Variables cannot be transferred using references in the call phase.
Parentheses are not allowed for return values.
Function Foo ()
{
// Incorrect syntax
Return ($ this-> bar );
// Correct statement
Return $ this-> bar;
}
4.5.2. Usage of functions and methods
If a function has multiple parameters, you need to add a space after each Comma. See the following example:
Threearguments (1, 2, 3 );
In the call phase, parameter passing by reference is not allowed, but should be placed in the function definition phase.
For functions that allow the use of array parameters, function calls allow the use of array declaration statements, and can be divided into multiple rows. At the same time, indentation is required to maintain readability. For example:
Threearguments (Array (1, 2, 3), 2, 3 );
Threearguments (Array (1, 2, 3, 'zend', 'studio ',
$ A, $ B, $ C,
56.44, $ D, 500), 2, 3 );
4.6. Control statement 4.6.1. If/else/elseif
After the keyword "if" and "elseif" in the control statement, a space must be separated from the left brace. There must also be a space after the right brace.
For conditional statements in parentheses, there must be spaces on both sides of the operator to ensure readability. If there are many conditions in the brackets, we recommend that you add parentheses based on logical grouping.
The left braces should be written in the same row of the Condition Statement, and the right braces should be placed in one row, and the content inside the brackets should be indented by four characters.
If ($! = 2 ){
$ A = 2;
}
For if statements that contain elseif or else, the format requirements are as follows:
If ($! = 2 ){
$ A = 2;
} Else {
$ A = 7;
}
If ($! = 2 ){
$ A = 2;
} Elseif ($ A = 3 ){
$ A = 4;
} Else {
$ A = 7;
}
Although PHP allows these statements to not use braces in some cases, this is not allowed in our encoding specifications. All if, elseif, and else statements must use braces.
Although the elseif structure is allowed, we recommend that you use the "else if" combination.
4.6.2. Switch
For the control statement switch, it is used to enclose the Left and Right brackets of the Condition Statement. both the left and right brackets must have a space.
The content inside the switch must be indented by four spaces, and the content under each case statement must also be indented by four spaces.
Switch ($ numpeople ){
Case 1:
Break;
Case 2:
Break;
Default:
Break;
}
The switch statement must have a default statement and cannot be omitted.
Note:In some cases, to allow a case statement to jump directly to the next case statement after the operation is completed, it intentionally removes the break or return statement. To distinguish this situation from a bug, it is recommended that you add a note where the break or return needs to be omitted: "// here the break intentionally omitted Statement (break intentionted) is intentionally removed )". 4.7. Internal documentation 4.7.1. Document Format
All document blocks (that is, docblocks) must follow the phpjavasentor format, here do not introduce phpjavasentor format, please refer to the website http://phpdoc.org
All source code files written for or used by Zend framework must contain file-Level Document blocks at the top of each file, and contain Class-Level Document blocks on top of each class definition, the following is an example of the document block.
4.7.2. File-Level Document Blocks
Any file containing PHP code must contain a document block at the top of it and contain at least the following phpdocumentor flag:
/**
* Brief description of this document
*
* Detailed description of this file (if any )...
*
* License: license information
*
* @ Copyright 2005 Zend Technologies
* @ License http://www.zend.com/license/3_0.txt PHP license 3.0
* @ Version CVS: $ ID: $
* @ Link http://dev.zend.com/package/PackageName
* @ Since file available since release 1.2.0
*/
4.7.3. Class-Level Document Block
Each document block at the class level must contain at least the following phpdocumentor labels:
/**
* Brief description of the class
*
* Class description (if any )...
*
* @ Copyright 2005 Zend Technologies
* @ License http://www.zend.com/license/3_0.txt PHP license 3.0
* @ Version release: @ package_version @
* @ Link http://dev.zend.com/package/PackageName
* @ Since class available since release 1.2.0
* @ Deprecated class deprecated in release 2.0.0
*/
4.7.4. function-Level Document Block
Each function, including object methods, must contain at least the following document blocks:
L Function Description
L all parameters
L all possible values returned
You do not need to use the @ access flag here, because the access level has been specified for keywords such as public, private, and protected used to declare the function.
If a function or method may throw an exception, use the @ throws: Mark, for example:
@ Throws exceptionclass [description]