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.
Always use full-form of PHP code tags:
<?php ?>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');
Use 4 spaces instead of tabs for an indent.
view.php base_dao.php my_super_class.php
class MySuperClass { //code here } class PREFIX_MySuperClass { //code here }
function connect() function camelCaseFunction() function fooBar()global functions
function my_global_function()
public $myVar; private $hisVar; protected $x;
define("MY_MEGA_CONSTANT", "Hello world");
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 ( condition ) { case 1: action1(); break; case 2: action2(); break; default: defaultAction(); break; }
Use else if statement instead of elseif
if ( $a !== $b ) { return false; } else if ( false ) { doSomething1(); } else { doSomething2(); }
Split long if statements onto several lines
if ( condition1 || condition2 && condition3 ) { //code here }
foreach ( $a as $v ) { echo $v; }
try { //code here } catch ( Exception $e ) { //code here }
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 }
myCoolFunction(1, 2, 3); $this->myCoolMethod(1, 2, 3);
$assoc = array ( 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' );