PHP Code Specification Summary _php Tutorial

Source: Internet
Author: User
Naming conventions
Θ files are named with the. class.php suffix, using the Hump method, and capitalized, such as Pay.class.php;
The Θ class name and directory _ file name are identical. 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 the underscore "_", such as Listcomment (), _getresource (), the method usually preceded by an underscore belongs to the private method;
The Θ attribute is named using the Hump method, the first letter lowercase or using the underscore "_", such as $username,$_instance, the attribute usually preceded by an underscore belongs to the private property;
Θ constants are named with uppercase letters and underscores "_", such as "Home_url";
Common nouns
1>list nouns (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 rule 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 results
Code Refactoring
1. The code in the function or method body is as far as possible within one screen.
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
With && | | Simplify operations
Before simplifying:
Copy CodeThe code is as follows:
$a = 1;
$b = 0;
if (Isset ($a)) {
$b = 1;
Print ($b. " \ n ");
}
if ($b!=0) {
Print ($b. " \ n ");
}

After simplification:
Copy CodeThe code is as follows:
$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 "= =", put the constant in front
Before:
Copy CodeThe code is as follows:
$a = 1;
if ($a = 1) {
echo ' $a = = 1 ';
}

After:
Copy CodeThe code is as follows:
$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:
Copy CodeThe code is as follows:
$a = 1;
if (1 = = $a) {
echo ' $a = = 1 ';
}

Lookup Table Method
Before:
Copy CodeThe code is as follows:
/* ERROR code: 4,5,7,8 returns status 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 CodeThe code is as follows:
/* ERROR code: 4,5,7,8 returns status 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";

The obvious code is more condensed, clearer, easier to understand, and faster!
Summarize
I wanted to put some design patterns into the usual code, but too much, not too good to put. These are just micro-parts!
If you have a better wording, you can leave a message.

http://www.bkjia.com/PHPjc/325259.html www.bkjia.com true http://www.bkjia.com/PHPjc/325259.html techarticle 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. Example: Class name Zend_au ...

  • 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.