PHP Code Standard

PSR-1 (Basic Coding Standard)

  • PHP Tags — must use <?php ?>
  • Character Encoding — only UTF-8 without BOM
  • Side Effects — php should do one thing declaration or work
  • Namespace and Class Names — follow PSR-0, PSR-4
  • Class Constants, Properties, and Methods
    • Constants — upper case with underscore separators
    • Properties — recommended lower case with underscore separators
    • Methods — camel case with lower case in first character

PSR-2 (Coding Style Guide)

  • Overview
    • must follow PSR-1
    • use indent with 4 spaces not tabs
    • line length should be 80 – 120 characters
    • should be one blank line after namespace declaration
    • should be one blank line after use declaration
    • use declearation must go after namespace declearation
    • opening/closing braces for classes, must go on next line
    • opening/closing braces for method, must go on next line
    • visibilities (public, private, protected) must declear on every properties and methods
    • abstract and final must declared before the visibilities
    • static must decleared after visibilities
    • control structures (if, else, while, for, foreach, …) must have one space after them
    • opening braces for control structures must go on the same line
    • opening parentheses for control structures must not have a space after them
    • closing parentheses for control structures must not have a space before them
  • General
    • Iines
      • must not be trailing whitespace at the end of non-blank lines
      • blank line may be added to improve readability
      • there must not be more than one statement per line
    • Indenting — must use 4 spaces
    • Keywords and True/False/Null — lower case
    • Classes, Properties, and Methods
      • Extends and Implements
        • extends and implements must be decleared on the same line
        • if we have to implements so many imterfaces, should declared in multiple lines with one interface per line
      • Properties
        • not use var for declared properties
        • one property declared per line
        • propertied should not be prefix with a single underscore
      • Methods
        • method should not be prefix with a single underscore
      • Method Arguments
        • must not be a space before each comma
        • must be one space after comma
        • argument with default value must go at the end of the argument list
        • if we there alot of argument list, it can split across the line with one argument per line, the closing parenthesis and opening brag should be placed together on their own line

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s