Summary of PHP code specifications
Summary of PHP code specifications
Naming rules
All the hosts files are suffixed with. class. php. They are named by the camper method and capitalized, for example, Pay. class. php;
The category class name and the Directory _ file name are consistent. For example, the Directory of the class name Zend_Autoloader is Zend/Autoloader. class. php;
The alias function uses lowercase letters and underscores. Example: get_client_ip;
The upper method uses the hump method, and the first letter is lowercase or the underline "_", for example, listComment (), _ getResource (). Generally, the method starting with an underscore is a private method;
The name of the marker attribute uses the hump method. The first letter is lowercase or the underline "_", for example, $ username, $ _ instance. Generally, attributes starting with an underscore are private attributes;
A constant named after an uppercase letter and an underscore "_", for example, "HOME_URL ";
Common Terms
1> list nouns (singular), such as listApple, read the apple list at first glance. We do not need to write them as getApples, listApples, or readApples-because get is generally used to read a single data, for example, getApple. listApples does not add s. We also know that the apple list is used (ensure that the variable name is shortened as much as possible );
2> get nouns (singular );
3> the term "Total" indicates the Total number of items. Such as expenseTotal;
4> found: Indicates whether a value has been found;
5> uccess or OK: whether an operation is successful;
6> done: whether a project is completed;
7> error: Indicates whether an error has occurred;
8> result: returned results
Code Reconstruction
1. The code in the function or method body should be controlled on a screen as much as possible.
2. Random deletion of unused methods in the class.
3. Modify the methods in others' classes and sign them.
4. Write a readme file in each module (for complex business instructions or code instructions ).
5. Try to make every class do its own thing, and every function does one thing.
Common Code
Use & or | to simplify operations
Before simplification:
The Code is as follows:
$ A = 1;
$ B = 0;
If (isset ($ )){
$ B = 1;
Print ($ B. "\ n ");
}
If ($ B! = 0 ){
Print ($ B. "\ n ");
}
After simplification:
The Code is as follows:
$ A = 1;
$ B = 0;
Isset ($ a) & ($ B = 1) & print ($ B. "\ n ");
$ B = 0 | print ($ B. "\ n ");
Obviously, the code looks more neat and simpler!
When determining "=", put the constant in front
Before:
The Code is as follows:
$ A = 1;
If ($ a = 1 ){
Echo '$ a = 1 ';
}
After:
The Code is as follows:
$ A = 1;
If (1 = $ ){
Echo '$ a = 1 ';
}
Obviously, if constants are placed in front, the compiler will be able to judge errors.
Regular format:
The Code is as follows:
$ A = 1;
If (1 = $ ){
Echo '$ a = 1 ';
}
Searching tables
Before:
The Code is as follows:
/* Error code: Status 1 is returned for, and status 2 is returned for, 6 */
$ Error = 4;
$ State = 0;
If ($ error = 4 | $ error = 5 | $ error = 7 | $ error = 8 ){
$ State = 1;
}
If ($ error = 1 | $ error = 3 | $ error = 6 ){
$ State = 2;
}
Echo "$ state \ n ";
After:
The Code is as follows:
/* Error code: Status 1 is returned for, and status 2 is returned for, 6 */
$ Error = 4;
$ State = 0;
$ Arr = array (4 => 1, 5 => 1, 7 => 1, 8 => 1, 1 => 2, 3 => 2, 6 => 2 );
Isset ($ arr [$ error]) & ($ state = $ arr [$ error]);
Echo "$ state \ n ";
Obviously, the code is more concise, clearer, easier to understand, and faster!
Summary
I originally wanted to place any design patterns in common code, but there were too many patterns that were not easy to put. These are just microservices!
If you have a better way of writing, you can leave a message.