PHP Code Specification Summary _php Foundation

Source: Internet
Author: User
Naming conventions
The
θ class files are suffix with. class.php, named with the Hump method, and capitalized, for example, Pay.class.php;


θ class Name and directory _ filename are consistent. For example: The Directory of class name Zend_autoloader is zend/autoloader.class.php;

The
θ function is named using lowercase letters and underscores. For example: GET_CLIENT_IP;

The
θ method is named using the Hump method, the first letter lowercase or using an underscore "_", such as Listcomment (), _getresource (), the method that usually begins with an underscore is a private method;

The name of the
θ property uses the Hump method, the first letter lowercase or the underscore "_", such as $username,$_instance, where the attribute at the beginning of the underscore is a private property;


θ constants are named with uppercase letters and underscore "_", such as "Home_url";


common noun


1>list noun (singular), such as listapple, we know that reading the Apple list, we do not need to write getapples or listapples or readapples--because get we specify 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 naming);


2>get nouns (singular);


3> A noun total that represents the sum of something. such as Expensetotal;


4>found: Indicates whether a value has been found;


5>uccess or OK: an operation is successful;


6>done: Whether a project is completed;


7>error: Whether there is a mistake occurred;


8>result: Returned results


Code Refactoring
1. The code in the function or method body is controlled as much as possible within a screen.


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


3. Modify the methods in other people's classes to sign.


4. Write a Readme file in each module (a description or code description for the more complex business).


5. Try to make each class do its own thing, each function to do one thing.


Common Code


&&/| | Simplify Operations


before simplification:


Copy Code code as follows:

$a = 1;
$b = 0;
if (Isset ($a)) {
$b = 1;
Print ($b. " \ n ");
}
if ($b!=0) {
Print ($b. " \ n ");
}



after simplification:


Copy Code code as follows:

$a = 1;
$b = 0;
Isset ($a) && ($b =1) && print ($b. " \ n ");
$b = 0 | | Print ($b. " \ n ");






obvious code looks more neat, simpler!


when judging "= =", put the constant in front

Before
:


Copy Code code as follows:

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


After
:


Copy Code code as follows:

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



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


Formal Format:


Copy Code code as follows:

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



look-up Table Method

Before
:


Copy Code code as follows:

/* ERROR code: 4,5,7,8 return State 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
:


Copy Code code as follows:

/* ERROR code: 4,5,7,8 return State 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";



obvious code is more concise, clearer, easier to understand, faster!


Summary


originally wanted to put what design pattern into common code, but too much, not too good to put. These are just micro-parts!


If you have a better way to write, you can leave a message.

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.