Tuesday 4 June 2013

php coding standards

1. Indenting and Line Lengths
·         Use indent of four spaces, no tab spaces(set tab size = 4 spaces)
·         It is recommended to keep lines at approximately 75-80 characters long for better code readability.

2. Control Structures
·         If , for, while , switch comes under control structure
·         <?php
if ((condition1) || (condition2)) {
    action1;
} elseif ((condition3) && (condition4)) {
    action2;
} else {
    defaultaction;
}
?>
·         Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls.
·         Split long if statements onto several lines
<?php
if (($condition1
    || $condition2)
    && $condition3
    && $condition4
) {    //code here}?>
·         Ternary operators
The same rule as for if clauses also applies for the ternary operator.
<?php
$a = $condition1 && $condition2
    ? $foo : $bar;
?>

3.Function Calls
·         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.

<?php
$var = helloworld($bar, $baz, $quux);
?>
·         Split function call on several lines

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.

·         Alignment of assignments

To support readability, the equal signs may be aligned in block-related assignments
<?php
$var1  = foo($bar);
$var2 = foo($baz);
?>
Note : The rule can be broken when the length of the variable name is at least 8 characters longer/shorter than the previous one

·         Split long assignments onto several lines

Assignments may be split onto several lines when the character/line limit would be exceeded. The equal sign has to be positioned onto the following line, and indented by 4 characters.
4. Class Definition
·         Class declarations have their opening brace on a new line.
<?php
class Foo_Bar
{
    //... code goes here
}
?> 

5. Function Definitions
·         Function declarations follow the "K&R style":
<?php
function fooFunction($arg1, $arg2 = '')
{
    if (condition) {
        statement;
    }
    return $val;
}
?>
           
·         Split function definitions onto several lines
1.     Functions with many parameters may need to be split onto several lines to keep the 80 characters/line limit.
2.     The first parameters may be put onto the same line as the function name if there is enough space. 
3.      Subsequent parameters on following lines are to be indented 4 spaces.
4.     The closing parenthesis and the opening brace are to be put onto the next line, on the same indentation level as the "function" keyword.
5.     <?php
function someFunctionWithAVeryLongName($firstParameter ='something', $secondParameter = 'booooo',
    $third = null, $fourthParameter = false, $fifthParameter = 123.12,
    $sixthParam = true
) {
    //....
?>

6. Arrays
·         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',
);
?>

7.Comments
·         Complete inline documentation comment blocks (docblocks) must be provided.
1.     C style comments (/* */) 
2.     standard C++ comments (//)     both are fine.

8.PHP Code Tags
·         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.

9. Examples URLs
·         Use example.com, example.org and example.net for all example URLs and email addresses

10.Naming Conventions
·         Global Variables and Functions
1.    If your package needs to define global variables, their names should start with a single underscore followed by the package name and another underscore.
$_XML_destructor_object_list
2.    Global functions should be named using the "studly caps" style (also referred to as "bumpy case" or "camel caps"). 
XML_RPC_serializeData ()
·         Classes
Classes should be given descriptive names. Avoid using abbreviations where possible. Class names should always begin with an uppercase letter. The PEAR class hierarchy is also reflected in the class name, each level of the hierarchy separated with a single underscore. Examples of good class names are
Log                         Net_Finger                               HTML_Upload_Error
·         Class Variables and Methods
Class variables (a.k.a properties) and methods should be named using the "studly caps" style (also referred to as "bumpy case" or "camel caps"). Some examples (these would be "public" members)


$counter         connect()              getData()

No comments: