PHP code specification, php code specification _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP code specifications: php code specifications. PHP code specification, php code specification naming of the class objects all use. class. php is a suffix. It is named by the camper method and capitalized, for example, Pay. class. php; class name and directory _ File name: PHP code specification; php code specification
Naming conventions the. class. php class files are all suffixed with. class. php. They are named in the upper case by using the hump method, for example, Pay. class. php; the names of the classes are the same as those of the directory _ file names. For example, the Directory of the class name Zend_Autoloader is Zend/Autoloader. class. php; the alias function uses lowercase letters and underscores. For example, the get_client_ip method uses the hump method, the first letter is lowercase, or the underscore "_", 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; the comment constant is named by uppercase letters and underscores (_), such as "HOME_URL". Common Terms 1> list nouns (singular), such as listApple, read the Apple list at a glance, we do not need to write it as getApples, listApples, or readApples -- because get is generally used to read a single data, such as getApple. if listApples is not added with s, we also know that it is an Apple List (ensure that the variable name is shortened as much as possible); 2> get noun (singular); 3> noun Total, indicating the Total number of items. For example, 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: whether an error occurs. 8> result: the returned result code is restructured. 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 Codes & | before simplified operations:
$a=1;$b = 0;if(isset($a)){ $b=1; print($b."\n");} if($b!=0){ print($b."\n");}

After simplification:

$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 "=", we may write "=" as "=". it is difficult to debug such a bug. Therefore, the compiler can determine whether to put constants in front. Before:
$a = 1;if($a = 1){ echo '$a == 1';}

After:

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

Obviously, if constants are placed in front, the compiler will be able to judge errors.

Regular format:

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

Before searching the table:
/* 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:
/* 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!

Http://www.bkjia.com/PHPjc/1107664.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1107664.htmlTechArticlePHP code specification, php code specification naming convention class files are. class. php is a suffix. It is named by the camper method and capitalized, for example, Pay. class. php; class name and directory _ file name 1...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.