PHP Coding Specification

Source: Internet
Author: User
Tags naming convention

first, file format

1. For files that contain only PHP code, we will ignore "?>" at the end of the File. This is to prevent extraneous spaces or other characters from affecting the Code.
For example:
<?php
$foo = ' Foo ';
2. Indentation should be able to reflect the logical results of the code, use four spaces as much as possible, and disable tab tab, as this guarantees flexibility across the client programmer Software.
For example:
if (1 = = $x) {
$indented _code = 1;
if (1 = = $new _line) {
$more _indented_code = 1;
}
}
3. Variable assignments must be equally spaced and arranged.
For example:
$variable = ' Demo ';
$var = ' Demo2 ';
4. Each line of code should be within 80 characters and up to 120 characters in Length. Because the Linux read-in file is generally listed in 80 units, that is, if a line of code more than 80 characters, then the system will pay extra instructions for This. While this may seem like a minor problem, it is a standard that deserves attention and adherence to the perfect programmer.
5. No extra spaces are allowed at the end of each LINE.

second, the naming convention

1. Class files are suffixed with ". class.php", and the class file name allows only letters, which are named with the hump method, and capitalized, such as:dbmysql.class.php .
2. Files other than other class library files such as configuration and functions are typically suffixed with ". inc.php" and ". php", and the file name is named using lowercase letters and underscores, with multiple words separated by an underscore, such as config.inc.php   common.php,install_function.php .
3. Ensure that file naming and invocation are case-sensitive, because they are case-insensitive on unix-like systems.
4. The class name and file name are the same (including the above-mentioned capitalization), and the class name only allows letters, such as the  useraction class file name is useraction.class.php,  The file name for the Infomodel class is infomodel.class.php .
5. The Controller class is suffixed with an action, such as  useraction, infoaction , and the model class is suffixed with models, such as usermodel, infomodel  Other classes are also suffixed with corresponding classifications, such as service , Widgets.
6. Method names are only allowed by letter, underline is not allowed, first letter to lowercase, followed by the first letter of each word to capitalize, that is, the so-called   "hump law named"   rules, and the more detailed the better, should be able to describe the function of the method, such as switchmodel, Findpage.
7. The name of the property is only allowed by the letter, the underscore is not allowed, the first letter to lowercase, followed by the first letter of each word to capitalize, that is, the so-called   "hump law named"   rules, such as tableprefix, tablename .
8. For access to object members, we must always use the   "get"   and   "set"   METHODS. For example:
class foo
{
    protected $_testobj;
    public function gettestobj ()

{
Return $this->_testobj;
}
Public Function Settestobj ($testObj)

{
$this->testobj = $_testobj;
}
}
9. When a class member method is declared as private, it must begin with a double underscore "__", respectively, and must be preceded by a single underscore "_" when declared as protected; the method is not Underlined. For example:
Class Foo
{
Private Function __example ()

    {
// ...
}
protected function _example ()
    {
// ...
}
Public Function Example ()
{
// ...
}
}
10. If we need to define some of the frequently used methods as global functions, then we should define them in the class statically (static). For example:
class Think
{
// ...
static public function AutoLoad ($classname)

{
// ...
}
}
11. The class member property declared as private must begin with a double underscore "__", and the class member property declared as protected must begin with an underscore "_", whereas a member property declared as public is not allowed to have an underscore at any Time.
12. The function is named with lowercase letters and underscores, and the more detailed the better, the function should be able to be described clearly, such as Get_client_ip.
13. Use "null" instead of "false" as the default value for a function parameter when the method or function parameter does not necessarily need to be assigned, unless the parameter is a Boolean Value.
14. variables are only allowed to be composed of lowercase letters and underscores, and the proposed naming of descriptive variables, the more detailed the better, so that like $i or $n, etc. are discouraged to use.
15. Constants in the class constant and globally in constant define, can only be composed of uppercase letters and underscores, each word is divided by an underscore.
Both the Boolean value and the null value are Lowercase.

third, Coding Style

1. PHP code must be bound in full form (<?php ...?). >), i.e. do not use PHP short tags (<? ... ? >) and ensure that no spaces are available after the label is Closed.
2. When a string is composed of plain text (that is, does not contain a variable), it must always be a single quotation mark (') as the Delimiter. For example:
$a = ' Example String ';
3. Variables in variable substitution are only allowed in the form of $+ variable Names. For example:
$greeting = "Hello $name, Welcome back!"; Allow
$greeting = "Hello {$name}, Welcome back!"; Allow
$greeting = "Hello ${name}, Welcome back!"; Not allowed
When using the dot "." to concatenate strings, the string must be separated from the dot with a single space and allowed to be split into multiple lines to enhance Readability. In this case, the dot "." must be aligned with the equals sign "=". For example:
$sql = "select ' id ', ' name '". " From ' People ' "
. "WHERE ' name ' = ' Susan '"
. "ORDER by ' name ' ASC";
When you construct an array with an array type symbol, you must add a space after each comma to enhance Readability. For example: $sampleArray = array (1, 2, 3, ' Think ', ' SNS ');
4. When declaring an associative array using the array type, we encourage dividing it into multiple rows, except that we must simultaneously ensure that the keys of each row are aligned with the value to keep it Beautiful. For example:
$sampleArray = Array (
' Firstkey ' = ' firstvalue ',
' Secondkey ' = ' secondvalue '
);
5. The opening of the curly brace must be shelf on the next line of the class Name. For example:
class Think
{
// ...
}
6. All code in the class must be indented with four Spaces.
7. Each PHP file allows only one class to be Declared. It is permissible to write other code in the class file, but it is not encouraged to do so. If you really want to attach the code, you must separate it with a blank line.
8. Declarations of any class variable must be placed at the top of the class, preceded by the declaration of any Function.
9. The Var notation is not allowed to declare variables, and class member variables must be declared with private,protected and Public. second, It is generally better to use the get and set methods to access class members, although it is permissible to declare class members as public and refer directly to Them.
10. The method must always declare its scope with private,protected or public.
11. Static Statics methods should declare their scope and should not be declared private privately, but should be protected or public, and if they do not want to inherit from the quilt class, they should be declared with Final.
12. The initial curly brace of a function or method should be shelf on the next line of the function Declaration. For example:
function Get_client_ip ()
{
...
}
13. No extra spaces are allowed between the function or method name and the parameter Brackets. For example:
function Get_client_ip ()
{
...
}
14. References are only allowed to be defined in function arguments, and real-time pass-through references are Forbidden. For example:
Reference definitions in function arguments-allowed
function Definerefinmethod (& $a)
{
$a = ' a ';
}
Definerefinmethod ($b);
Echo $b; A
Real-time delivery of References-forbidden
function Calltimepassref ($a)
{
$a = ' a ';
}
Calltimepassref (& $c);
Echo $c; A
15. Function or method return value can not be wrapped in parentheses, otherwise it will reduce readability, and if later function is modified to return a reference, this will throw an Exception.
16. Encourage the use of type hints as much as possible, especially in module designs. For example:
class Foo
{
Public function foo (someinterface $object)

   {
}
Public Function Bar (array $options)
    {
}
}
17. function and method parameters must be separated by commas + spaces.
18. for a function whose parameters are arrays, the array in the parameter should be divided into multiple lines to enhance Readability. For example:
threearguments (array (1, 2, 3), 2, 3);
Threearguments (array (1, 2, 3, ' Think ',
' SNS ', $a, $b, $c,
56.44, $d, 500), 2, 3);
19. based on the condition control of "if", "else" and "else if", We must open the statement and parentheses with a space interval, the opening of the curly brace "{" must be on the same line as the conditional control statement, the end "}" must always be exclusive one row and shelf, the control flow content must be indented with four spaces, And do not use "elseif".
if ($condition) {
// ...
} else if ($_condition) {
// ...
} else {
// ...
}
20. within the conditional parentheses of the conditional control statement, the operator must be separated from the other elements with a space. If you encounter a long logical decision, you are encouraged to use the enclosing parentheses to separate the Logic. For example:
if ($a! = 2) and ($b = = 1)) {
$a = $b;
}
In the "switch" conditional control statement, The parameter to be measured must be separated from other elements with a space. For example:
Switch ($num) {
...
}
The contents of the "switch" statement must be indented with four spaces, and the content controlled by the "case" condition must be indented with four additional spaces. For example:
Switch ($indentedSpaces) {
Case 2:
Echo "error";
Break
Case 4:
Echo "right";
Break
Default
Break
}
23. The "default" control should always be included in the "switch" Statement.
24. Sometimes we need to omit "break" or "return" in the "case" context, at which point we must add "//no break here" comments for these "case" Statements. For example:
Switch ($numPeople) {
Case 1://no Break here

Case 2:
Break
Default
Break
}

PHP Coding 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.