Coding guidlines
Version 1.1 04.06.2015
Code styling and naming conventions
Version 1.1 04.06.2015
Code styling and naming conventions
Guidline of how to write PHP code.
Basic instructions - Variable that holds a directory: NO / -symbol in the end: 'my/dir'. Same as: dirname(FILE)
Coding patterns | Quidlines of coding structure.
Name | Case type |
---|---|
ClassName | PascalCase |
methodName() | camelCase |
$propertyName | camelCase |
function_name() | lower_case |
$variable_name | lower_case |
$ObjectVariable | PascalCase |
$tempVariable | Temporary variable |
$nameArray | If is a need to clarify type of the variable |
CONSTANT_NAME | UPPER_CASE |
<?php
/**
* Name of the class
*
* Indent with 4 space, no tabs.
* One empty row after description.
*
* @method int magicFunction() Methods defined by interception.
* @property className $magicProperty Properties defined by interception.
*/
namespace My/Component;
use Other/Component;
use Other/Component as Com; // Alias
class ModelClass extends OtherClass implements SomeInterface
{
use My/Utility/CommonTrait;
/** @var string $parameter Description of property */
public $parameter = 'default';
protected static $parameter = 'default'; // Do NOT prefix with _
/**
* Purpose of the method
*
* @since 2012-10-30
* @todo What is still needed to be done.
*
* @param string $string Parameter description.
* @return string What is this returned string.
*/
final public static function setName($string = '', $number)
{
return $this->addText($string);
}
}
// Newline after function/method definition.
// No PHP end tag '?>' in the end of the file.
Single quotes. Concatenation in beginning of the next row.
$var = 'Single <DIV class="important">quotes</DIV>'
.'With additional '
.$this->methodCall()
.' Concat in beginning of the row';
$obj = $mad->get('ui', 'icon')
->title('Title')
->icon('plus');
Own rows, indented. Ending in own row.
$array = Array(
'name' => 'Nimi',
'value' => 'Arvo'
);
Space after a comma in function argument list.
addText($var, '100');
else to its own row.
// Description of if
if ( $condition ){
// Do something
}
// Description of else
else if ( $otherCondition ){
// Do other thing
}