Programming standards


Variable names

  1. The first letter of a variable must start with lowercase
  2. Variables native or arrays must be separated by underscore(_) every word change: $my_var
  3. The variables that are Objects each word end must be capitalized, example: $myVar
  4. Global variables must be all capitalized, example: $MY_VAR

Assignments

  1. There must be a space between the variable and the operators:

 

$my_var = 17; $a = $b;

 

Operators

  1. "+", "-", "*", "/", "=" and any combination of them (eg "/ =") need a space between
    members of the left and right

    $a + 17;

    $result = $b / 2;

    $i + = 34;

  2. «.» no space between the left and right limbs
    echo $a.$b;

    $c = $d.$Este-> foo ();

    Recommendation

    For performance reasons, do not exceed the use of concatenation.

  3. ". =" Needs a space between the left and right members
    $a. = 'Debug';

Declarations

  1. if, elseif, while, for: must have a space between the if keyword and the parentheses
    if ( )

    while ( )

  2. When a combination of if and else else is used and if both must offer a value, the else must be avoided.
    if ( )

        return false;

    return true;

    Recommendation

    We recommend one result per method / function

  3. When a method / function offers a boolean value and the current value of the method / function depends on it, the if statement should be avoided
    public aFirstMethod ()

    {

        return $Este-> aSecondMethod ();

    }

  4. The tests should be grouped by "entity"
    if ($price AND! Empty ($price))

        [...]

    if (! Validate :: $myObject OR $myObject-> id === NULL)

        [...]

Visibility

  1. Visibility must be defined at all times, even when it is a public method.
  2. The order of the method properties must be: visibility static function name ()
    private static function foo ()

Method / Function Names

  1. Method and function names always start with a lowercase character and each of the following words must start with an uppercase character (CamelCase)
    public function myExempleMethodWithALotOfWordsInItsName ()
  2. The brackets that are presented in the method code have to be preceded by a return
    public function myMethod ($arg1, $arg2)

    {

        [...]

    }

  3. Los nombres de los métodos y las funciones deben ser nombres explícitos, por lo tanto nombres de función como «b()» o «ef()» están completamente prohibidos.

    Excepciones

    The only exceptions are the translation function called "l ()" and the debugging functions "p ()", "d ()".

Enumeration

Commas must be followed (and only followed) by a space.

protected function myProtectedMethod ($arg1, $arg2, $arg3 = null)

Objects / Classes

  1. The name of the object must be singular
    class Customer
  2. The class name must follow the CamelCase model, except that the first letter is capitalized
    class MyBeautifulClass

Definitions

  1. Definitions names must be entered in uppercase
  2. The names of the definitions must have the prefix "PS_" inside the kernel and the module
    define ('PS_DEBUG'1);

    define ('PS_MODULE_NAME_DEBUG'1);

  3. Definition names do not allow alphabetic characters. Except "_".

Keywords

All keywords must be lowercase
ex. as, case, if, echo, null

Constants

Constants must be in uppercase, except "true" and "false" and "null", which must be in lowercase
ex. "ENT_NOQUOTE", "true"

Configuration variables

Configuration variables follow the same rules as definitions

Chains

Strings must be enclosed in single quotes, never double

threw out 'Debug';

$myObj-> name = 'Hello '.$name;

Comments

  1. Within functions and methods, only the comment "//" tag is allowed
  2. After the comment tag «//», a space "// Comment" is required
    // My great comment
  3. The comment tag "//" is allowed at the end of a line of code
    $a = 17 23// A comment inside my exemple function
  4. External functions and methods, only the «/" and "/ »Comments are allowed
    / * This method is required for compatibility issues * /

    public function foo ()

    {

    // Some code explanation right here

    [...]

    }

  5. Comment PHP Doc Element is required before method declarations
    /**

     * Return field value if possible (both classical and multilingual fields)

     *

     * Case 1: Return value if present in $_POST / $_GET

     * Case 2: Return object value

     *

     * @param object $obj Object

     * @param string $key Field name

     * @param integer $id_lang Language id (optional)

     * @return string

     */

    protected function getFieldValue ($obj, $key, $id_lang = NULL)

    For more information

    For more information about the PHP Doc rule: see this useful link

Return of values

  1. Return declarations do not need parentheses, except for a compound expression
    return $result;

    return ($a + $b);

    return (a () - b ());

    return true;

  2. Disarm a function
    return;

Call

The calling function preceded by an "@" is prohibited but be careful when calling the function / method with login / password or argument path.

myfunction ()

// In the following example we put an @ for security reasons

@mysql_connect([...]);

Labels

  1. A blank line should be left after the PHP opening tag
    <?php

     

    require_once ('my_file.inc.php');

  2. Final PHP tag is forbidden

Indentation

  1. El carácter de tabulación («t») es el único caracter de indentación que se permite
  2. Each level of indentation must be represented by a single tab character
    function foo ($a)

    {

        if ($a == null)

            return false;

        [...]

    }

Matrix

  1. The array keyword must not be followed by a space
    array (172342);
  2. The indentation should be as follows when a lot of data is inside an array:
    $a = array (

        36 => $b,

        $c => 'foo',

        $d => array (172342),

        $e => array (

            0 => 'zero',

            1 => $one

        )

    );

Block

Square brackets are prohibited when defining only one statement or statement combination

if (! $result)

    return false;

 

for ($i = 0; $i < 17; $i ++)

    if ($myArray [$i] == $value)

        $result [] = $myArray [$i];

    else

        $failed ++;

Previous 18 CSS Libraries to Create Flirty Animations and Effects
Next Custom events in jQuery

No Comment

Leave a reply

Your email address will not be published. Required fields are marked *