Novice PHP Coding Specifications and recommendations

Source: Internet
Author: User
If you want to be an excellent programmer, good code is important, how can you write good code? In this article we will give you some PHP coding specifications and recommendations.

Coding specifications

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

<?php//start//Not end

    • PHP code files must be encoded with UTF-8 without a 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 can have side effects (such as generating file output and modifying. ini configuration files, etc.), only one option.

Example, A.phpclass a{}b.phpfunction demo () {}c.phpdefine (' a ', value);d. Phpini_set (' Some_vars ', value);

    • The naming of class/trait/interface must follow 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 ...}

    • The private method (in class/trait) names must conform to the _camelcase-style front-underlined lowercase header of the hump naming convention.

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

    • There are 1 spaces between the method/function multi-parameter

Class studlycaps{public    function dosomething ($variableOne, $variableTwo)    {        //coding ...    }}

    • Operator/expression must 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 introduce class//empty line

    • The opening curly brace "{" Of a 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), 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 <=), while ($valueFive <=) {  //code ...} for ($i = 0; $i < $valueSix; $i + +) {  //code ...} $demo = new Demo () $demo->dosomething ();d o_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 <=), while ($valueFive <=) {  //code ...} for ($i = 0; $i < $valueSix; $i + +) {  //code ...}

    • There must be no whitespace in the start of the control structure and before the closing parenthesis.

if ($valueOne = = = $valueTwo) {//Control structure (right Bien Hoa) left without space  //code ...}

Coding recommendations

    • SQL too long

Heredoc syntax $sql = <<<sqlselect delivery_idfrom d_testwhere delivery_idin (123,234) GROUP by delivery_idhaving 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 a 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 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

Overwrite dosome to Dosomethingclass 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 file

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

The above content to the novice programmer some of the code and suggestions for writing, hope to help everyone.

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.