PHP Coding Specifications and recommendations

Source: Internet
Author: User


# # # Encoding Specification

-PHP code files must start with the <?php tag.

```
<?php//Start

Not ending
```

-PHP code files must be encoded with UTF-8 without BOM.

```
Example sublime, setting increased, "show_encoding": True
```

-the number of characters per line is less than 80 characters

```
Example, Sublime
"Word_wrap": "true",
"Wrap_width": 80,
```

-Tap Key 4 spaces

```
Example, Sublime
"Tab_size": 4,
```

-The PHP code should only define classes (trait)/functions/constants/Other actions that produce side effects (such as: Generate file output and modify. ini config file, etc.), only one option.

```
Cases
a.php
Class A
{

}

b.php
Function Demo ()
{

}

c.php
Define (' A ', value);

d.php
Ini_set (' Some_vars ', value);
```

-Class/trait/interface must be named according to the Hump naming specification at the beginning of studlycaps capitalization.

```
Class StudlyCaps
{

}

Trait StudlyCaps
{

}

Interface StudlyCaps
{

}

```

-Constants in a class all letters must be capitalized, and the words are separated by underscores.

```
Define (' Foo_bar ', ' something More ');

Const Foo_bar = value;

```

-The name of the method (in class/trait) must conform to the CamelCase-style lowercase start hump naming specification.

```
Class StudlyCaps
{
Public Function StudlyCaps ()
{
Coding ...
}
}

```

-the function name must conform to the Snake_case-style underline naming specification.

```
function Snake_case ()
{
Coding ...
}

```

-Private method (in class/trait) the name must conform to the _camelcase-style pre-underscore lowercase header of the Hump naming specification.

```
Class StudlyCaps
{
Private Function _studlycaps ()
{
Coding ...
}
}

```

-Method Name The first word is a verb.

```
Class StudlyCaps
{
Public Function dosomething ()
{
Coding ...
}
}

```

-the variable must conform to the CamelCase-style lowercase start hump naming specification.

```

Class StudlyCaps
{
Public Function dosomething ()
{
$someVariable = ' demo ';
Coding ...
}
}

```

-When method/function multi-parameter, there are 1 spaces between

```

Class StudlyCaps
{
Public Function dosomething ($variableOne, $variableTwo)
{
Coding ...
}
}

```

-operator/expression to have a space

```
$a = $b + $c;
$a = $b. $c;

```

-After each namespace namespace declaration statement block and the use declaration statement block, you must insert a blank line.

```
namespace Standard;
Empty line
Use Test\testclass;//use Introduction class
Empty line
```

-The opening curly brace "{" Of the class must be written in a line after the function declaration, and the closing curly brace "}" must also be written in a row after the function body.

```
Class StudlyCaps
{

}
```

-The opening curly brace of the method/function {must be written in a line after the function declaration, closing the curly brace} must also be written in a line after the function body.

```
Class StudlyCaps
{
Public Function StudlyCaps ()
{
Coding ...
}
}

function Snake_case ()
{
Coding ...
}

```

-The properties and methods of the class must add access modifiers (private, protected, and public), and abstract and final must be declared before the access modifier,


and static must be declared after the access modifier.

```
Abstract class StudlyCaps
{
Abstract public Function studlycaps ();

Final public static function Studlycapsone ()
{

}
}

```

-You must have a space character after the key for the control structure, but you cannot call a method or function.

```
if ($valueOne = = = $valueTwo) {
Code ...
}

Switch ($valueThree) {
Case ' value ':
Code ...
Break

Default
Code ...
Break
}

do {
Code ...
} while ($valueFour <= 10);

while ($valueFive <= 10) {
Code ...
}

for ($i = 0; $i < $valueSix; $i + +) {
Code ...
}

$demo = new Demo ()
$demo->dosomething ();

Do_something ();

```

-The opening curly brace of the control structure {must be written on the same line as the declaration, and the closing curly brace} must be written in a row after the body.

```
if ($valueOne = = = $valueTwo) {
Code ...
}

Switch ($valueThree) {
Case ' value ':
Code ...
Break

Default
Code ...
Break
}

do {
Code ...
} while ($valueFour <= 10);

while ($valueFive <= 10) {
Code ...
}

for ($i = 0; $i < $valueSix; $i + +) {
Code ...
}
```

-There must be no space between the opening parenthesis of the control structure and before the closing parenthesis.

```
if ($valueOne = = = $valueTwo) {//Control structure (right Bien Hoa) left without spaces
Code ...
}
```

# # # Coding Recommendations

-SQL Too long

```
Heredoc syntax
$sql = <<<sql
SELECT delivery_id
From D_test
WHERE delivery_id
In (123,234)
GROUP by delivery_id
Having SUM (Send_number) <= 0;
SQL;
```

-If the control structure condition is too long

```
if ($a > 0
&& $b > 0
&& $c > 0
&& $d > 0
&& $e > 0) {

}
```

-method or function parameter is greater than three line breaks

```
Public Function Toolangfunction (
$valueOne = ' ',
$valueTwo = ' ',
$valueThree = ' ',
$valueFour = ' ',
$valueFive = ' ',
$valueSix = ")
{
Coding ...
}
```

-chain operation of more than two

```
$this->nametest->functionone ()
->functiontwo ()
->functionthree ();
```

-After array php5.4, use []

```
$a = [
' AAA ' = ' aaa ',
' BBB ' = ' BBB '
];
```

-Single quote multi-quote
-No variables in string, single quotes
-There are variables in the string, double quotes

```
$str = ' str ';
$arg = "$str";
```

-Declare class or method or function add description & attribute Description & author

```
/**
* Class Description
*
* DESC
*/
Class Standardexample
{
/**
* Constant description.
*
* @var String
*/
Const THIS_IS_A_CONST = ";

/**
* Attribute description.
*
* @var String
*/
Public $nameTest = ';

/**
* Constructor function.
*
* Constructor Function description
* @author name <email>
* @param string $value parameter name/description
* @return return value type return value description
* Return value type: string,array,object,mixed (multiple, indeterminate), void (no return value)
*/
Public function __construct ($value = ")
{
Coding ...
}
```

-API method provides test sample example

```
/**
* Member method name.
*
* Member Method description
*
* @param string $value parameter name/description
*
* @example domain/api/controller/action?argu1=111&argu2=222
*/
Public Function testfunction ($value = ")
{
Code ...
}
```

-Use Try...catch ...

```
try {

Coding ...

} catch (\exception $e) {
Coding ...
}

```

-Continuous invocation of multiple methods (greater than 3) using foreach

```
Rewrite Dosome to DoSomething
Class Standardexample
{
/**
* Method List
*
* @var Array
*/
Private $_functionlist = [];

Public function __construct ($functionList = Array ())
{
$this->_functionlist = $value;
}

Public Function Dosome ()
{
$this->functionone ();
$this->functiontwo ();
$this->functionthree ();
$this->functionfour ();
}

Public Function dosomething ()
{
foreach ($this->_functionlist as $function) {
$this $function ();
}
}

...
}
```

-Copyright notice at the top of the document

```
// +----------------------------------------------------------------------
// | Company Name XX Service
// +----------------------------------------------------------------------
// | Copyright (c) http://domain All rights reserved.
// +----------------------------------------------------------------------
// | Author:name <email>
// +----------------------------------------------------------------------
```

PHP Coding Specifications and recommendations

Related Article

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.