Php Conventions and Coding Style

Only available on StudyMode
  • Topic: Regular expression, Bracket, Perl
  • Pages : 7 (1229 words )
  • Download(s) : 114
  • Published : January 13, 2013
Open Document
Text Preview
PHP Conventions and Coding Style
● ● File and Directory Names Brackets ○ Curly Brackets ○ Class Brackets ○ Empty Brackets ○ Array Brackets ■ Opening Parenthesis ■ Closing parenthesis ● Single Dimension ● Multidimensional ■ Arrays as Function Arguments Naming Conventions ○ Classes ○ Functions and Methods ○ Variables Indentation String Concatenation Single Line Statements Comparison Operations Switch Structures Parentheses Ternaries Type Casting Constants Comments ○ One-line Comments Regular Expressions



● ● ● ● ● ● ● ● ● ● ●

Reference: http://kohanaframework.org/3.2/guide/kohana/conventions Syntax Highlighter: https://snipt.net

File and Directory Names
1. CamelCased should not be used. 2. Should be lowercase. Example: Incorrect ThisIsDirectory/ThisIsDirectory/ThisIsFile.php Correct this_is_directory/this_is_directory/this_is_file.php

Coding Standards
In order to produce highly consistent source code, we ask that everyone follow the coding standards as closely as possible.

Brackets
Please use BSD/Allman Style bracketing. Example:
while (x == y) { something(); somethingelse(); }

The following is still syntactically correct:
//while (x == y) { something(); somethingelse(); }

As is this:
//for (int i=0; i < x; i++) //while (x == y) if (x == y) { something(); somethingelse(); }

Curly Brackets
Curly brackets are placed on their own line, indented to the same level as the control statement. // Correct if ($a === $b) { ... } else { ... } // Incorrect if ($a === $b) { ... } else { ... }

Class Brackets
The only exception to the curly bracket rule is, the opening bracket of a class goes on the same line. // Correct class Foo { // Incorrect class Foo {

Empty Brackets
Don't put any characters inside empty brackets.
// Correct class Foo {} // Incorrect class Foo { }

Array Brackets
Arrays may be signle line or multi-line
array('a' => 'b', 'c' => 'd') array( 'a' => 'b',

'c' => 'd', )

Opening Parenthesis The opening array parenthesis goes on the same line. // Correct array( ... ) // Incorrect: array ( ... )

Closing parenthesis
Single Dimension

The closing parenthesis of a multi-line single dimension array is placed on its own line, indented to the same level as the assignment or statement. // Correct $array = array( ... ) // Incorrect $array = array( ... )

Multidimensional

The nested array is indented one tab to the right, following the single dimension rules. // Correct array( 'arr' => array( ... ), 'arr' => array( ... ), ) array( 'arr' => array(...), 'arr' => array(...), )

Arrays as Function Arguments
// Correct do(array( ... )) // Incorrect do(array( ... ))

As noted at the start of the array bracket section, single line syntax is also valid. // Correct do(array(...)) // Alternative for wrapping long lines do($bar, 'this is a very long line', array(...));

Naming Conventions
Kohana uses under_score naming, not camelCase naming.

Classes
// Controller class, uses Controller_ prefix class Controller_Apple extends Controller { // Model class, uses Model_ prefix class Model_Cheese extends Model { // Regular class class Peanut {

When creating an instance of a class, don't use parentheses if you're not passing something on to the constructor: // Correct: $db = new Database; // Incorrect: $db = new Database();

Functions and Methods

Functions should be all lowercase, and use under_scores to separate words: function drink_beverage($beverage) {

Variables
All variables should be lowercase and use under_score, not camelCase: // Correct: $foo = 'bar'; $long_example = 'uses underscores'; // Incorrect: $weDontWantThis = 'understood?';

Indentation
You must use tabs to indent your code. Using spaces for tabbing is strictly forbidden. Vertical spacing (for multi-line) is done with spaces. Tabs are not good for vertical alignment because different people have different tab widths. $text = 'this is a long text block that is wrapped. Normally, we aim for '...
tracking img