====== Coding standards ======
If you decided to write code for Oxwall make sure you observe code style standards described in this convention.
You don't HAVE to make your code look fancy to write a great plugin but think how others looking at your code would appreciate if it's written alike the rest of the Oxwall code. All rules in this convention are not taken "because we like it so" but were elaborated with readability, portability, compatibility, fail-safety and general acceptability in mind.
===== General =====
Always use full-form of PHP code tags:
For files containing only PHP code, the closing tag **//?>//** must be ommitted.
**//include_once//** and **//require_once//** are statements, not functions. Parentheses should not surround the subject filename.
//RIGHT
require_once 'header.php';
//WRONG
require_once('header.php');
===== Indenting =====
Use **4 spaces** instead of tabs for an indent.
===== Naming conventions =====
==== Filenames ====
view.php
base_dao.php
my_super_class.php
==== Classes ====
class MySuperClass
{
//code here
}
class PREFIX_MySuperClass
{
//code here
}
==== Functions ====
function connect()
function camelCaseFunction()
function fooBar()
global functions
function my_global_function()
==== Vars ====
public $myVar;
private $hisVar;
protected $x;
==== Constants ====
define("MY_MEGA_CONSTANT", "Hello world");
===== Control structures =====
**//foreach, for, while, if, switch, try, catch//** etc.
There should be one space between the control keyword and opening parenthesis in control statements, which will distinguish them from function calls.
Use curly braces even in case they are optional, as this will make the code more readable and help to avoid logic errors appearing when new lines are added.
==== switch ====
switch ( condition )
{
case 1:
action1();
break;
case 2:
action2();
break;
default:
defaultAction();
break;
}
==== if, else ====
Use **//else if//** statement instead of **//elseif//**
if ( $a !== $b )
{
return false;
}
else if ( false )
{
doSomething1();
}
else
{
doSomething2();
}
==== long if statements ====
Split long **//if//** statements onto several lines
if ( condition1
|| condition2
&& condition3 )
{
//code here
}
==== foreach, for, while ====
foreach ( $a as $v )
{
echo $v;
}
==== try, catch ====
try
{
//code here
}
catch ( Exception $e )
{
//code here
}
===== Function declaration =====
All function names must be in a **camelCase**. Global functions are an exception. They should consist of words in lowercase and underscores.
There should be whitespaces before and after parameter list.
There should be no space between a function name and an opening parenthesis.
There should be a new line before return statement.
function fooBar( $param1, $param2 )
{
if ( $param1 !=== $param2 )
{
//code here
}
return true;
}
You should always declare a type of parameter when possible:
function doSomethingGood( MyClass $obj )
{
//code here
}
Global function example:
function print_var( $var, $echo = false )
{
//code here
}
===== Function calls =====
myCoolFunction(1, 2, 3);
$this->myCoolMethod(1, 2, 3);
===== Arrays =====
==== Indexed arrays ====
$arr = array ( 1, 2, 'no', 'pain', 'no', 'gain' );
$longArr = array ( 1, 2, 3,
4, 5, 6,
7, 8, 9 );
==== Associative arrays ====
$assoc = array ( 'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3' );