Site icon Osclass Documentation & Knowledge Base

Coding style

This small guide is partly based in the coding style guide of Zend Framework.

PHP Code Demarcation

PHP code must always be delimited by the full-form, standard PHP tags:

<?php
 
?>

Short tags are never allowed. For files containing only PHP code, the closing tag must always be omitted (See General standards).

Variables

Vairables should follow the Hungarian Notation ( https://en.wikipedia.org/wiki/Hungarian_notation ), that means they should start with a character indicating the type of variable, for example i for integer, a for arrays, o for objects, s for strings … Also, each first character of a word should be uppercase.

$iThisIsAnInteger = 42;
$sSomeText = 'This is some text';
$aVariable = array(1, 2, 3, 4 , 5);
...

Note: SQL column’s names should follow a similar notation, but the first character should follow an underscore. Words are separated by an underscore, all in lowercase. If the column is a primary key, it should be preceded by “pk_” if it’s a foreign key by “fk_”

i_integer_variable
s_some_text
dt_registration_date
pk_i_id // this IS a PRIMARY KEY
fk_i_category_id // this IS a FOREIGN KEY
...

Classes

Class Declaration

The brace should always be written on the line underneath the class name.

Every class must have a documentation block that conforms to the PHPDocumentor standard. The following is an example of an acceptable class declaration:

/**
* Documentation Block Here
*/
class Foo
{
    /**
     * Documentation Block Here
     */
    public function bar()
    {
        // all contents of function
        // must be indented four spaces
    }
}

Control Statements

If/Else/Elseif

Control statements based on the if and elseif constructs must have a single space before the opening parenthesis of the conditional and a single space after the closing parenthesis.

The opening brace is written on the same line as the conditional statement. The closing brace is always written on its own line. Any content within the braces must be indented using four spaces.

if ($a != 2) {
    $a = 2;
} else {
    $a = 7;
}
 
if ($a != 2) {
    $a = 2;
} elseif ($a == 3) {
    $a = 4;
} else {
    $a = 7;
}

It’s required ALWAYS to put the braces. The following if statements is not valid

if($a != 2)
    $a = 2;

Switch

Control statements written with the “switch” statement must have a single space before the opening parenthesis of the conditional statement and after the closing parenthesis.

All content within the “switch” statement must be indented using four spaces. Content under each “case” statement must be indented using an additional four spaces.

switch ($numPeople) {
    case 1:
        break;
 
    case 2:
        break;
 
    default:
        break;
}

The construct default should never be omitted from a switch statement.

Documentation Format

All documentation blocks (“docblocks”) must be compatible with the phpDocumentor format. Describing the phpDocumentor format is beyond the scope of this document.