当前位置:网站首页>PHP code specification

PHP code specification

2022-06-23 03:05:00 User 1349575

Naming specification

Θ Class files are .class.php For the suffix , Use the hump method to name , And capitalize the first letter , for example Pay.class.php;

Θ Class name and directory _ Consistent file names . for example : Class name Zend_Autoloader The directory of is Zend/Autoloader.class.php;

Θ Functions are named using lowercase letters and underscores . for example :get_client_ip;

Θ The method is named by hump method , The first letter is lowercase or underlined "_", for example listComment(),_getResource(), Usually, methods that start with an underline are private methods ;

Θ Attributes are named using the hump method , The first letter is lowercase or underlined "_", Such as $username,$_instance, In general, properties that begin with an underline are private properties ;

Θ Constants are capitalized and underlined "_" name , Such as "HOME_URL";

Common nouns

 1>list Noun ( singular ), Such as listApple, At first glance, we know to read the apple list , We don't have to write getApples perhaps listApples or readApples—— because get We specify that it is generally used to read single data , Such as getApple.listApples No addition s We also know it is to take the apple list ( Ensure that variable naming is as short as possible );

 2>get Noun ( singular );

 3> Noun Total, Indicates the total number of something . 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 has been completed ;

 7>error: Whether there is a mistake ;

 8>result: The result returned

Code refactoring

 1. Try to control the code in a function or method in one screen .

 2. Methods not used in the class are deleted randomly .

 3. Modify other people's methods in class , To sign .

 4. Write a in each module readme file ( Description or code description for complex business ).

 5. Try to let each class do its own thing , Each function does one thing .

Common code

use && or || Simplified operation

Before simplification :

$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 more tidy , It's simpler !

Judge "==" when , We may put "==" It's written in "=", In this way bug It is difficult for us to debug the diagram . therefore , Put the constants first , The compiler can tell .

Before :

$a = 1;if($a = 1){    echo '$a == 1';}

after :

$a = 1;if(1 = $a){    echo '$a == 1';}

obvious , If constants are put in front , The compiler can determine the error .

Normal format :

$a = 1;if(1 == $a){    echo '$a == 1';}

Look up table method

Before :

/* Error code :4,5,7,8 Return to the status when 1, The error code is 1,3,6 Return to 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 Return to the status when 1, The error code is 1,3,6 Return to 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 , More clearly , Easier to understand , Faster, too !

summary

I originally wanted to put some design patterns into common code , But too much , It's not easy to put . These are just small parts !

原网站

版权声明
本文为[User 1349575]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201242154044593.html