php coding standard

31
Created By Wasim Chowdhury Manager (In Operation) Infrablue Technology

Upload: wasim-chowdhury

Post on 26-Dec-2014

108 views

Category:

Documents


3 download

TRANSCRIPT

Created By Wasim ChowdhuryManager (In Operation)Infrablue Technology

It helps if the standard annoys everyone in some way so everyone feels they are on the same playing field.

The proposal here has evolved over many projects, many companies, and literally a total of many weeks spent arguing. It is no particular person's style and is certainly open to local amendments.

programmers can go into any code and figure out what's going on

new people can get up to speed quickly people new to PHP are spared the need to

develop a personal style and defend it to the death

people new to PHP are spared making the same mistakes over and over again

people make fewer mistakes in consistent environments

the standard is usually stupid because it was made by someone who doesn't understand PHP

the standard is usually stupid because it's not what I do

standards reduce creativity standards are unnecessary as long as people

are consistent standards enforce too much structure people ignore standards anyway

Efficiency

Maintainability

Collaboration

PEAR Coding Standards*

Zend Framework Coding Standards

eZcomponents Coding Standards

PHPLIB

PEAR PEAR standards are the most widely used

and accepted coding standards, in large part due to the number of people using PEAR as well as the general quality and explicitness of the standard.

Zend Framework Only to note the additions it makes to PEAR

standards.

Popular component library Issues have already been debated Well known and accepted (used more than any

other standard) Basis for many other projects

Horde (actually, based on Horde) Solar Zend Framework AdoDB

Use an indent of 4 spaces, with no tabs.

It is recommended to keep lines at approximately 75-85 characters long for better code readability.

<?phpif ((condition1) || (condition2)) {    action1;} elseif ((condition3) && (condition4)) {    action2;} else {    defaultaction;}?>

<?phpswitch (condition) {case 1:    action1;    break;

case 2:    action2;    break;

default:    defaultaction;    break;}?>

Its better to use this style for multiple nested control structure. if (){ ...}else{ ...}

Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis, and the semicolon. Here's an example:

<?php$var = foo($bar, $baz, $quux);?>

As displayed above, there should be one space on either side of an equals sign used to assign the return value of a function to a variable. In the case of a block of related assignments, more space may be inserted to promote readability:

<?php$short          = foo($bar);$long_variable = foo($baz);?>

To support readability, parameters in subsequent calls to the same function/method may be aligned by parameter name:

<?php$this->callSomeFunction('param1',     'second',        true);$this->callSomeFunction('parameter2', 'third',         false);$this->callSomeFunction('3',          'verrrrrrylong', true);

?>

The CS require lines to have a maximum length of 80 chars. Calling functions or methods with many parameters while adhering to CS is impossible in that cases. It is allowed to split parameters in function calls onto several lines.

<?php$this->someObject->subObject->callThisFunctionWithALongName(    $parameterOne, $parameterTwo,    $aVeryLongParameterThree);?>

Class declarations have their opening brace on a new line:

<?phpclass Foo_Bar{

    //... code goes here

}?>

<?phpfunction fooFunction($arg1, $arg2 = ''){    if (condition) {        statement;    }    return $val;}?>

Arguments with default values go at the end of the argument list. Always attempt to return a meaningful value from a function if one is appropriate. Here is a slightly longer example:

<?phpfunction connect(&$dsn, $persistent = false){    if (is_array($dsn)) {        $dsninfo = &$dsn;    } else {        $dsninfo = DB::parseDSN($dsn);    }

    if (!$dsninfo || !$dsninfo['phptype']) {        return $this->raiseError();    }

    return true;}?>

Functions with many parameters may need to be split onto several lines to keep the 80 characters/line limit. The first parameters may be put onto the same line as the function name if there is enough space. Subsequent parameters on following lines are to be indented 4 spaces. The closing parenthesis and the opening brace are to be put onto the next line, on the same indentation level as the "function" keyword.

<?php

function someFunctionWithAVeryLongName($firstParameter = 'something', 

$secondParameter = 'booooo',    $third = null, $fourthParameter = false, $fifthParameter = 123.12,    $sixthParam = true) {    //....?>

Assignments in arrays may be aligned. When splitting array definitions onto several lines, the last value may also have a trailing comma. This is valid PHP syntax and helps to keep code diffs minimal:

<?php

$some_array = array(    'foo'  => 'bar',    'spam' => 'ham',);?>

Non-documentation comments are strongly encouraged. A general rule of thumb is that if you look at a section of code and think "Wow, I don't want to try and describe that", you need to comment it before you forget how it works.

C style comments (/* */) and standard C++ comments (//) are both fine. Use of Perl/shell style comments (#) is discouraged.

Always use <?php ?> to delimit PHP code, not the <? ?> shorthand. This is required for PEAR compliance and is also the most portable way to include PHP code on differing operating systems and setups.

Class Names MixedCase Initial Cap Underscores separate logical package

and code boundaries Examples

Spreadsheet_Excel_Writer Services_Google_AdWords

PEAR Naming Conventions Variables camelCased initial character lowercase

Constants ALL_CAPS underscores for word separators

PEAR Naming Conventions Functions and Methods camelCased Initial character lowercase

Example:function isThatOk(){ statement;}

Readability of code blocks<?php

if ($foo) {    $bar = 1;}if ($spam) {    $ham = 1;}if ($pinky) {    $brain = 1;}

?>

<?php

if ($foo) {    $bar = 1;}

if ($spam) {    $ham = 1;}

if ($pinky) {    $brain = 1;}?>

Return early

<?php

function foo($bar, $baz){    if ($foo) {        //whole        //logic        return $calculated_value;    } else {        return null;    }}

?>

<?php

function foo($bar, $baz){    if (!$foo) {        return null;    }    //whole    //logic    return $calculated_value;}?>

?