Sunteți pe pagina 1din 5

PHP Coding Standards Bridge Global IT Staffing

Coding Standards - PHP


PHP Tags
PHP code MUST use the long <?php ?> tags. This is required for PHP compliance
and is also the most portable way to include PHP code on differing operating
systems and setups.

Class Names
Class names MUST be declared in StudlyCaps. Eg: TestClass
Namespaces
Namespaces and classes MUST follow PSR-0

Constants
Class constants MUST be declared in all upper case with underscore
separators. For example:
<?php
namespace Vendor\Model;
class Foo
{
const VERSION = '1.0';
const DATE_APPROVED = '2012-06-01';
}

Methods

Method names MUST be declared in camelCase().


It should be possible to tell the main purpose of a function just by looking at
the first line, e.g. get_user_data($username).
Function arguments should be separated by spaces, both when the function
is defined and when it is called. However, there should not be any spaces
between the arguments and the opening/closing brackets.

PHP Coding Standards Bridge Global IT Staffing

Variable Names
Variable names should be all lowercase, with words separated by underscores.
Names should be descriptive, but also concise. Wherever possible, keep variable
names to a minimum length

Global constants should be all caps with '_' separators.

Keywords
Keywords such as true, false, null, as, etc should be all lower case, as upper case
is reserved for constants. Same goes for primitive types like array, integer, string.
$var = true;
$var = false;
$var = null;
foreach ($array as $key => $value)
public function my_function(array $array)
function my_function($arg = null)

Including braces
Braces should always be included when writing code using if, for, while etc.
blocks. There are no exceptions to this rule, even if the braces could be
omitted. Leaving out braces makes code harder to maintain in the future and
can also cause bugs that are very difficult to track down.
Incorrect
if ( condition ) foo();

Correct
if ( condition )
{
foo();
}

PHP Coding Standards Bridge Global IT Staffing


Braces should also align properly (use two spaces to achieve this) so a closing brace
is always in the same column as the corresponding opening brace.
Control Structures - These include if, for, while, switch, etc. Control statements
should have one space between the control keyword and opening parenthesis, to
distinguish them from function calls. You are strongly encouraged to always use
curly braces even in situations where they are technically optional.
Example;
if ($arg1 == 'good' || $arg1 == 'fair') {
$this->foo = $arg1;
}

Function Calls - Functions should be called with no spaces between the function
name, the opening parenthesis, and the first parameter; spaces between commas
and each parameter, and no space between the last parameter, the closing
parenthesis, and the semicolon.
Example;
$this->setFoo($arg1, $arg2 = 0);

SQL code layout


When writing SQL queries, capitalize all SQL keywords (SELECT, FROM, VALUES,
AS etc.) and leave everything else in the relevant case.

Quoting strings
If your string contains no variables, use single quotes and save the parser the
trouble of attempting to interpolate the string for variables.

Shortcut operators

The shortcut operators ++ and -- should always be used on a line of their


own, with the exception of for loops.
Optional shortcut constructs
Usage of a ternary operator wherever possible: Eg:
$username = isset($_POST['username']) ? $_POST['username'] : '';

PHP Coding Standards Bridge Global IT Staffing


Use constants where possible

If a value is not going to change throughout the execution of your script,


then use a constant rather than a variable to define it.

Code Comments
Use comment blocks for classes and methods. Single line comments has to be used
where necessary.
A sample comment block for a class:
/**
* Test class - Controls the test actions
* @author Bridge
* @copyright Bridge India

*/

A sample method comment block:


/**
* Purpose of the method
* @param Array $data
* @param Int $id
* @return Boolean
*/

All properties defined in a class should also be commented:


/**
* Db connection
* @var $conn
*/

PHP Coding Standards Bridge Global IT Staffing


protected $conn;

Editor settings
Indenting and Line Length - Use an indent of 4 spaces and don't use any tab
because different editors use different setting for tab. It is recommended to keep
lines at approximately 75-85 characters long for better code readability.
PHP code MUST use only UTF-8

Segregation of php, html and scripts


If you are using custom frameworks or pure php , please note to separate html ,
php and javascript or jquery in separate sections . Note to follow the basic MVC rule
in any custom frameworks.
All business logic goes into the models, all actions go into the controllers and of
course the output/view goes into the views

S-ar putea să vă placă și