Skip to content

Dev.Coding Standard PHP

Taiwen Jiang edited this page Jul 15, 2013 · 23 revisions

PHP Coding Standard for Pi Engine

Based on Zend Framework 2 Coding Standards.

#PHP File Formatting

General

For files that contain only PHP code, the closing tag ?> MUST NOT be used. It is not required by PHP, and omitting it prevents the accidental injection of trailing white space into the response.

Indentation

Indentation MUST consist of 4 spaces. Tabs MUST NOT be used for indentation.

Maximum Line Length

The target line length is 80 characters. Developers SHOULD strive keep each line of their code under 80 characters. The maximum length of any line of PHP code is 120 characters.

Line Termination

Line termination follows the Unix text file convention. Lines MUST end with a single linefeed (LF) character. Linefeed characters are represented as ordinal 10, or hexadecimal 0x0A. Lines MUST NOT have whitespace characters preceding the linefeed character.

Naming Conventions

Class naming convention:

  • Namespaces have a 1:1 relationship to the filesystem.
  • Classes are stored within the directory determined by the namespace. The root directory is "lib", and contains the Pi/, Zend/, vendor directory, which corresponds to the top-level namespaces within Pi, Zend Framework and corresponding vendor libraries.

Namespaces

Namespaces MUST contain only alphanumeric characters, the underscore, and the namespace separator {{ }}.

Namespaces SHOULD be MixedCase, and acronyms used in namespaces SHOULD as well. As examples:

  • the namespace Zend\PDF would not be allowed, while Zend\Pdf is acceptable.
  • the namespace Zend\XMLRPC would not be allowed, while Zend\XmlRpc is acceptable.

Code deployed alongside Pi Engine libraries but which is not part of the Pi or Zend Framework standard distribution SHOULD utilize separate namespaces.

Namespace Aliases

When aliasing within ZF library code, the aliases SHOULD typically follow these patterns:

  • If aliasing a namespace, use the final segment of the namespace; this can be accomplished by simply omitting the as portion of the alias:
use Zend\Filter;       // Alias is then "Filter"
use Zend\Form\Element; // Alias is "Element"
  • If aliasing a class, either use the class name (no as clause), or suffix the class with the subcomponent namespace preceding it:
use Zend\Controller\Action\HelperBroker; // aliased as "HelperBroker"
use Zend\Filter\Int as IntFilter;        // using suffix

Classes

Abstract Classes

Interfaces

Filenames

Functions and Methods

Variables

Constants

Coding Style

PHP Code Demarcation

Strings

String Literals

String Literals Containing Apostrophes

Variable Substitution

String Concatenation

Class Names In Strings

Arrays

Numerically Indexed Arrays

Associative Arrays

Namespaces

Namespace Declaration

Import Statements

Classes

Class Declaration

Class Member Variables

Exceptions

Using Exceptions

Functions and Methods

Function and Method Declaration

Closure Definitions

Function and Method Usage

Control Statements

If/Else/Elseif

Switch

Inline Documentation

Documentation Format

General Notes

Files

Classes

Functions

Clone this wiki locally