Programming standards


imagen_php

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. “+”, “-“, “*”, “/”, “=” y cualquier combinación de ellos (ej. “/=”) necesitan un espacio entre
    members of the left and right

    $a + 17;

    $result = $b / 2;

    $i + = 34;

  2. “.” no llevan espacio entre los miembros de la izquierda y la derecha
    echo $a.$b;

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

    Recommendation

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

  3. “.=” necesita un espacio entre los miembros de la izquierda y la derecha
    $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. Las pruebas deben ser agrupadas por “entidad”
    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

    Las únicas excepciones son la función de traducción llamada “l()” y las funciones de depuración “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. Los nombres de las definiciones deben contar con el prefijo “PS_” dentro del núcleo y el módulo
    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

Las constantes deben estar en mayúsculas, excepto “verdadero” y “falso” y “nulo”, que debe estar en minúsculas
ej. “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. Dentro de las funciones y métodos, sólo la etiqueta “//” de comentario es permitida
  2. Después de la etiqueta “//” de comentario, un espacio “// Comment“ es necesario
    // My great comment
  3. La etiqueta “//” de comentario es permitida al final de una línea de código
    $a = 17 23// A comment inside my exemple function
  4. Funciones y métodos externos, sólo las etiquetas “/” y “/” de comentario son permitidas
    / * 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

La función de llamada precedida por una “@” está prohibida pero tenga cuidado con la llamada a la función / método con login / contraseña o ruta de argumentos.

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 cute animations and effects 18 CSS libraries to create cute animations and effects
Next Create Email Responsive with MJML Framework

No Comment

Leave a reply

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