Project

General

Profile

Actions

Coding conventions

General rule of thumb

While PHP is a server side scripting language that gets interpreted by a machine, it's humans that do the programming. We therefore think PHP-code should at all times be as readable as possible. What follows is a list of rules and suggestions to improve that readability. In general: if adding whitespace, linefeeds or blank lines makes the code more transparent and readable, then do it.

Naming conventions

File system

  • folders
    • lowercase
    • alphanumerical
    • underscores as separators
  • files
    • lowercase
    • alphanumerical
    • underscores as separators
  • (special) file extensions
    • classes: .class.php
    • included php: .inc.php
    • internationalization files: .i18n
    • templates: .tpl

PHP code

  • classes
    • camelcase
    • no underscores
    • first letter also capitalized
  • methods
    • lowercase
    • underscores as seperators
  • properties
    • lowercase
    • underscores as seperators
  • constants
    • uppercase
    • underscores as seperators

Code ordering

  • Outside classes
    1. namespace statement
    2. use statement(s)
    3. require / include statement(s)
  • Inside classes
    1. constants
    2. properties
    3. constructor
    4. getters / setters
    5. "the rest"
  • No closing tag for php-only files

Indentation

  • Tab policy: spaces
  • Indentation size: 4
  • Always indent the following:
    • Declarations within the class body
    • Statements within a method / function body
    • Statements within blocks
    • Statements withing switch bodies
    • Statements within case bodies
    • Break statements

Brace positions

Always put braces on the next line, without extra indentation.

Typically braces would be used for:
  • class or interface declarations
  • method declarations
  • blocks
  • switch statements

Inserting white space

Declarations

  • Classes
    • before the opening brace of a class
    • after a comma in the implements clause
  • Fields
    • after the comma in multiple field declarations
    • after the comma in multiple constant declarations
  • Methods
    • before the opening brace
    • after the comma in parameters

Control statements

  • Blocks
    • before and after the opening brace
  • if else
    • before the opening parenthesis
  • for
    • before the opening parenthesis
    • after the comma
    • after the semicolon
  • for each
    • before the opening parenthesis
    • before and after the arrow
  • switch
    • before the colon in the case
    • before the colon in the default
    • before the opening brace
    • before the opening parenthesis
  • while & do while
    • before the opening parenthesis
  • catch
    • before the opening parenthesis
  • static
    • after the comma
  • global
    • after the comma
  • echo
    • after the comma

(The white space before the opening parenthesis of control structure differentiate the control structure from the function calls/declaration)

Expressions

  • Field access
    • before and after the double colon in the field access
  • Method invocations
    • after the comma in method arguments
    • before and after the double colon in method invocations
  • Assignments
    • before and after the assignment operator
  • Operators
    • before and after binary operators
    • after unary operators
    • after prefix operators
    • before postfix operators
  • Type casts
    • after the closing parenthesis
  • Conditionals
    • before the question mark
    • after the question mark
    • before the colon
    • after the colon
  • Parenthesized expressions
    • no whitespace

Arrays

  • after the common in array creation
  • before and after the arrow in array creation
  • after the comma in a list
  • no whitespace before and after the opening brackets
  • no whitespace before the closing bracket
  • no whitespace between empty brackets

Inserting blank lines

  • no blank line before class declarations
  • no initial blank line in a class bodt
  • 1 blank line before constant declarations with comments
  • 1 blank line before field declarations with comments
  • 1 blank line between groups of constant and field declarations with or without comments
  • 1 blank line between blocks of one or more namespace, use and require/include statements
  • 1 blank line between the entirity of namespace, use and require/include statements and the class declaration

Inserting new lines

  • in empty class bodies
  • in empty method bodies
  • in empty blocks
  • before else and elseif in an if statement
  • before catch in a try statement
  • before while in a do statement

Line wrapping

  • Try to keep lines of less than 80 chars. Do not exceed 120 chars
  • Only wrap class declarations when necessary, use the default indentation
  • Only wrap the implements clause when necessary, use the default indentation
  • Only wrap method declarations and parameters when necessary, use the default indentation
  • Only wrap function calls, arguments and object allocation arguments when necessary, use the default indentation
  • Only wrap (binary) expressions and array initializers when necessary, use the default indentation

End of line style

If at all possible, force your IDE to use Unix-style end of line characters. Please consider using the Mercurial EolExtension (http://mercurial.selenic.com/wiki/EolExtension) to handle automatic conversion of end of line characters in your commits.

IDE templates

Certain IDE's allow the user to configure a code formatting template making it easy or easier to format your code consistently. Templates are available for:

  • Zend Studio 6/7/8
  • Netbeans IDE

If you use a different IDE which also allows for such templates, please share them with the rest of the community.

Updated by Anonymous over 11 years ago ยท 12 revisions