Php Conventions and Coding Style

Topics: Regular expression, Bracket, Perl Pages: 7 (1229 words) Published: January 13, 2013
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: Syntax Highlighter:

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.

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( ... )


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.

// 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) {

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

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 '...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Vhdl Coding Style for Digital Design Essay
  • Coding Conventions Essay
  • Coding Essay
  • Essay about Style
  • The Theatrical Styles and Conventions Used in Australian Plays Essay
  • Coding paper
  • Coding Essay
  • inpatien coding (penn foster exam) Essay

Become a StudyMode Member

Sign Up - It's Free