PHP Team Coding Specification & Code style specification

Source: Internet
Author: User
Tags try catch

first, the Basic Agreement1. Source Files

(1), Pure PHP code source files only use <?php tags, omit to close the label?>;

(2), the encoding format of PHP code in the source file must be UTF-8 format without BOM;

(3), using Unix LF (newline character) as the line terminator;

(4), a source file is only one type of declaration, that is, this file is specifically used to declare class, the file is specifically used to set configuration information, do not mix together to write;

2. Indent

Use the TAB key to indent, with each tab key length set to 4 spaces;

3, line

A line recommendation is to write up to 120 characters, more than this character should be wrapped, the general editor can be set.

4. Keywords and true/false/null

The PHP keyword must be lowercase, Boolean: true,false,null must also be lowercase.

Below is the "keyword" of PHP, which must be lowercase:

' __halt_compiler ', ' abstract ', ' and ', ' array ', ' as ', ' break ', ' callable ', ' case ', ' Catch ', ' class ', ' Clone ', ' const ', ' con Tinue ', ' Declare ', ' Default ', ' die ', ' do ', ' echo ', ' Else ', ' elseif ', ' empty ', ' enddeclare ', ' endfor ', ' Endforeach ', ' endif ' ', ' endswitch ', ' endwhile ', ' eval ', ' exit ', ' extends ', ' final ', ' for ', ' foreach ', ' function ', ' global ', ' goto ', ' If ', ' imp Lements ', ' include ', ' include_once ', ' instanceof ', ' insteadof ', ' interface ', ' isset ', ' list ', ' namespace ', ' new ', ' or ', ' print ', ' private ', ' protected ', ' public ', ' require ', ' require_once ', ' return ', ' static ', ' switch ', ' throw ', ' trait ', ' try ', ' unset ', ' use ', ' var ', ' While ', ' XOR '

5. Naming

(1), the class name uses the big hump type (studlycaps) to use the notation;

(2), (Class) method name using small hump (camecase) notation;

(3), function name using lowercase letter + underline notation, such as function http_send_post ();

(4), variable names using small hump writing, such as $userName;

second, the code style1. Namespaces (Namespace) and import (use) Declarations

The following is a simple text description:

    1. The Declaration of the namespace (namespace) must be followed by a blank line;
    2. All the import (use) declarations must be placed under the namespace (namespace) declaration;
    3. In a statement, there must be only one import (use) keyword;
    4. There must be a blank line after the import (use) Declaration code block;

Use the code to illustrate the following:

<?phpnamespace lib\databases; The following must be a space line class mysql{}

namespace a blank line before you can declare class by using use and an empty line.

<?phpnamespace lib\databases; The following must be a blank line use Foointerface; Use must declare the use Barclass as Bar;use Othervendor\otherpackage\bazclass after namespace; The following must be a space line class mysql{}
2. Classes (class), Attributes and Methods (method)

(1), inheritance (extends) and implementation (implement) must be written on one line with class name, and curly braces should be written in a newline.

<?phpnamespace Lib\databaes;class Mysql extends ParentClass implements \PDO, \db//write a line {//newline write {}

(2), the property must declare its visibility, whether public or protected or private, can not be omitted, and can not use Var, var is the old version of PHP in what way, and so on public.

<?phpnamespace Lib\databaes;class Mysql extends ParentClass implements \PDO, \db//write a line {public $foo = Null;private $nam E = ' Yangyi ';p rotected $age = ' 17 ';}

(3), method, must declare its visibility, whether public or protected or private, can not be omitted. Also, curly braces {must be wrapped and written. If there are multiple parameters, the first parameter is immediately followed by "," and a space is added: Function_name ($par, $par 2, $pa 3), and if the parameter has a default value, the "=" is separated by a space between the left and right.

<?phpnamespace Lib\databaes;class Mysql extends ParentClass implements \PDO, \db//write a line {public getInfo ($name, $age, $ge NDEr = 1)//parameter has a space between them. The default parameter of "=" has a space around {//must wrap to write {}}

(4), when using abstract and finalization (final) to make class declarations, they must be placed before the visibility declaration (public or protected or private). When static is used to make a class declaration, it must be placed behind the visibility declaration.

Directly on the code:

<?phpnamespace vendor\package;abstract class classname{protected static $foo;//static on the back abstract protected function Zim (); Abstract put front final public static function bar ()//final put front, static put last. {//Method body part}}
3. Control Structure

The control interface is the if else while switch. This type of code is often prone to problems, but also to standardize.

(1), if,elseif,else writing, directly on the code bar:

<?phpif ($expr 1)//If with (there is a space between {///must wrap write {}elseif ($expr 2)///Elesif to write, with (there is a space between {//must be wrapped write {}else{//must wrap write {}

(2), switch,case note the space and newline, or directly on the canonical code:

<?phpswitch ($expr)//switch with (there is a space between {//must be wrapped to write {case 0:echo ' first box, with a break ';//Snap to break; Qi. Case 1:echo ' Second case, which falls through ';//No breakcase 2:case 3:case 4:echo ' third case, return instead of break '; Return;default:echo ' default case ';

(3), while,do while the wording is similar, on the code:

<?phpwhile ($expr)//While with (there is a space between {//newline write {}do{//newline write {} while ($expr);//And a space left and right

(4), for the wording of

<?phpfor ($i = 0; $i < $i + +)//For and (there is a space between the two-dollar operator "=", "<" there is a space around {//newline write {}

(5), the wording of foreach

<?phpforeach ($iterable as $key + $value)//foreach and (there is a space between "=" and "=" has a space around {//newline write {}

(6), try Catch

<?phptry{//NewLine Write {}catch (firstexceptiontype $e)//catch with (there is a space between {//newline write {}catch (otherexceptiontype $e)//catch With (there is a space between {//newline write {}
4. Comments

(1), line comment

Need to add a space later;

If//preceded by a non-null character, then//before a space is required;

(2) Function comment

The parameter name, attribute name, label text should be aligned up and down;

Add a blank line before the first label;

<?php/** * This was a sample function to illustrate additional PHP * formatter options. * * @param        $one the first   parameter * @param int    $two the   second parameter * @param string $three the Thir D parameter with a longer *                      comment to illustrate wrapping. * @return void * @author  52php.cnblogs.com * @license GPL */function foo ($one, $two = 0, $three = "String") {}

Reference:

Code Style Research: is the left curly brace wrapped???

PHP psr-[0-4] Code specification

PHP Team Coding Specification & Code style 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.