Home > Backend Development > PHP Tutorial > PHP code specifications, php code specifications_PHP tutorial

PHP code specifications, php code specifications_PHP tutorial

WBOY
Release: 2016-07-12 08:57:36
Original
923 people have browsed it

PHP code specification, php code specification

Naming specification ​ Θ Class files have the suffix .class.php, are named using camel case, and the first letter is capitalized, for example Pay.class.php; Θ The class name and directory_file name are consistent. For example: the directory of the class name Zend_Autoloader is Zend/Autoloader.class.php; Θ functions are named using lowercase letters and underscores. For example: get_client_ip; Θ Methods are named using camel case, with the first letter lowercase or underscore "_", such as listComment(), _getResource(). Usually methods starting with an underscore are private methods; Θ Attributes are named using camel case, with the first letter lowercase or underscore "_", such as $username, $_instance. Usually attributes starting with an underscore are private attributes; Θ Constants are named with capital letters and underscores "_", such as "HOME_URL"; ​ ​ common nouns 1>List noun (singular), such as listApple, we know at a glance that we are reading a list of apples. We do not need to write getApples or listApples or readApples - because we stipulate that get is generally used to read a single data, such as getApple.listApples does not add We also know that s is to get a list of apples (make sure to shorten the variable naming as much as possible); 2>get noun (singular); 3>The noun Total means the total number of something. Such as expenseTotal; 4>found: Indicates whether a certain value has been found; 5> success or ok: whether an operation was successful; 6>done: Whether a certain project is completed; 7>error: Whether an error occurred; 8>result:The returned result ​ code refactoring ​ 1. Try to keep the code in the function or method body within one screen. 2. Unused methods in the class are randomly deleted. 3. To modify methods in other people’s classes, you need to sign them. 4. Write a readme file in each module (for more complex business descriptions or code descriptions). 5. Try to let each class do its own thing and each function do one thing.

Common codes ​ Use && or || to simplify operations ​ 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 neater and simpler!

When judging "==", we may write "==" as "=". It is difficult for us to debug such bugs. Therefore, put the constant in front and the compiler can judge it. ​ Before:
$a = 1; if($a = 1){     echo '$a == 1'; }
After

:

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

Obviously, if the constant is placed in front, the compiler will be able to determine the error.

Formal format:

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

​ lookup table method ​ Before:
/*错误码:4,5,7,8的时候返回状态1,错误码是1,3,6返回状态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:
/*错误码:4,5,7,8的时候返回状态1,错误码是1,3,6返回状态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!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1107664.htmlTechArticlePHP code specification, PHP code specification naming specification class files are suffixed with .class.php and named using camel case , and the first letter is capitalized, such as Pay.class.php; class name and directory_file name one...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template