PHP Code Specification

Source: Internet
Author: User

The naming specification Theta class files are suffixed with. class.php, are named with the Hump method, and capitalized, such as the Pay.class.php;θ class name and the directory _ filename. For example, the directory of class name Zend_autoloader is the way the zend/autoloader.class.php;θ function is named using lowercase letters and underscores. For example: The Get_client_ip;θ method is named using the Hump method, the first letter lowercase or using the underscore "_", such as Listcomment (), _getresource (), usually the underscore method is the private method, the Θ attribute named using the Hump method,  The first letter lowercase or use underscore "_", such as $username,$_instance, the attribute that usually begins with an underscore is a private property; Θ constants are named with uppercase letters and underscores "_", such as "Home_url"; Commonly used nouns 1>list nouns (singular), such as Listapple, a look we know to read the Apple list, we do not have to write getapples or listapples or readapples--because get we rule generally used to read a single data, If Getapple.listapples does not add s We also know is to take the Apple list (guaranteed to shorten the variable name); 2>get noun (singular); 3> noun total, which represents the sum 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 is completed; 7>error: Whether there is an error occurred; 8>result: Returned result code refactoring 1. The code in the body of the function or method should be controlled in one screen as much as possible. 2. Methods that are not used in the class are randomly deleted. 3. Modify other people's methods in the class, to sign. 4. Write a Readme file within each module (for more complex business instructions or code descriptions). 5. Try to make each class do its own thing, and do one thing for each function.

Common Code && or | | Simplified operation before Simplification:
$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");

The obvious code looks more neat and simpler!

When judging "= =", we may write "=" as "=", which makes it difficult for us to try to make a bug. So, put the constants in front and the compiler can tell. Before:
$a= 1;if($a = 1){    echo‘$a == 1‘;}

After:

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

Obviously, the compiler can judge the error when the constants are placed in front of them.

Formal format:

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

Before looking up the table method:
/*错误码: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";

The obvious code is more condensed, clearer, easier to understand, and faster!

PHP Code Specification

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.