Summary of the PHP code specification
- 2020-05-16 06:30:11
- OfStack
Naming conventions
Θ class files with class. php suffix, use hump method of naming, and capitalize the first letter, such as Pay. class. php;
Θ class name and directory _ filename to 1. For example: the name of the class Zend_Autoloader directory is Zend/Autoloader class. php;
Θ function named use lowercase letters and underscores. For example: get_client_ip;
Θ method named by using the method of hump first letters lowercase or use an underscore "_", for example listComment (), _getResource (), usually underscore methods belong to the private method;
Θ attribute names by using the method of hump first letters lowercase or use an underscore "_", such as $username, $_instance, usually underscore the properties belong to private property;
Θ constants with a capital letter and underscore "_", named as "HOME_URL";
Common nouns
1 > We don't have to write "getApples" or "listApples" or "readApples" because we specify "get" to read a single piece of data. "getApple.listApples" without "s" we also know that "list" is a list of apples.
2 > get noun (singular);
3 > The noun Total means the total number of something. Such as expenseTotal;
4 > found: indicates whether a value has been found;
5 > uccess or ok: whether the operation was successful or not;
6 > done: whether a project is completed;
7 > error: whether there is an error;
8 > result: the result returned
Code refactoring
1. Try to control the code inside the function or method within 1 screen.
2. Random deletion of methods not used in class.
3. Modify a method in someone else's class by signing it.
4. Write an readme file (a description or code description for a more complex business) inside each module.
5. Try to make each class do its own thing and each function do one thing.
Commonly used code
Simplify operations with && or ||
Simplify the front:
Simplified:
Obviously the code looks much neater and simpler!
When you say "==", put the constant first
Before:
After:
Obviously, if you put the constant first, the compiler will be able to judge the error.
Formal format:
A lookup table method
Before:
After:
Obviously the code is more concise, clearer, easier to understand, and faster!
conclusion
I wanted to put any design patterns in the code as usual, but there were too many of them. These are just the micro parts!
If you have a better way to write, you can leave a message.
Θ class files with class. php suffix, use hump method of naming, and capitalize the first letter, such as Pay. class. php;
Θ class name and directory _ filename to 1. For example: the name of the class Zend_Autoloader directory is Zend/Autoloader class. php;
Θ function named use lowercase letters and underscores. For example: get_client_ip;
Θ method named by using the method of hump first letters lowercase or use an underscore "_", for example listComment (), _getResource (), usually underscore methods belong to the private method;
Θ attribute names by using the method of hump first letters lowercase or use an underscore "_", such as $username, $_instance, usually underscore the properties belong to private property;
Θ constants with a capital letter and underscore "_", named as "HOME_URL";
Common nouns
1 > We don't have to write "getApples" or "listApples" or "readApples" because we specify "get" to read a single piece of data. "getApple.listApples" without "s" we also know that "list" is a list of apples.
2 > get noun (singular);
3 > The noun Total means the total number of something. Such as expenseTotal;
4 > found: indicates whether a value has been found;
5 > uccess or ok: whether the operation was successful or not;
6 > done: whether a project is completed;
7 > error: whether there is an error;
8 > result: the result returned
Code refactoring
1. Try to control the code inside the function or method within 1 screen.
2. Random deletion of methods not used in class.
3. Modify a method in someone else's class by signing it.
4. Write an readme file (a description or code description for a more complex business) inside each module.
5. Try to make each class do its own thing and each function do one thing.
Commonly used code
Simplify operations with && or ||
Simplify the front:
$a=1;
$b = 0;
if(isset($a)){
$b=1;
print($b."\n");
}
if($b!=0){
print($b."\n");
}
Simplified:
$a=1;
$b = 0;
isset($a) && ($b=1) && print($b."\n");
$b == 0 || print($b."\n");
Obviously the code looks much neater and simpler!
When you say "==", put the constant first
Before:
$a = 1;
if($a = 1){
echo '$a == 1';
}
After:
$a = 1;
if(1 = $a){
echo '$a == 1';
}
Obviously, if you put the constant first, the compiler will be able to judge the error.
Formal format:
$a = 1;
if(1 == $a){
echo '$a == 1';
}
A lookup table method
Before:
/* Error code :4,5,7,8 Returns the status when 1, Error code is 1,3,6 Return status 2*/
$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:
/* Error code :4,5,7,8 Returns the status when 1, Error code is 1,3,6 Return status 2*/
$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!
conclusion
I wanted to put any design patterns in the code as usual, but there were too many of them. These are just the micro parts!
If you have a better way to write, you can leave a message.