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

Class names MUST contain only alphanumeric characters. Numbers are permitted in class names but are discouraged in most cases. Underscores are not permitted in Pi.

If a class name is comprised of more than one word, the first letter of each new word MUST be capitalized. Successive capitalized letters are not allowed, e.g. a class "Pi\MVC" is not allowed while "Pi\Mvc" is acceptable.

Abstract Classes

Abstract classes follow the same conventions as classes, with one additional rule: abstract class names SHOULD begin with the term, ·Abstract·. As examples, ·AbstractAdapter· and ·AbstractWriter· are both considered valid abstract class names.

Abstract classes SHOULD be in the same namespace as concrete implementations. The following would be considered invalid usage: ··· namespace Zend\Log;

abstract class AbstractWriter implements Writer { } ··· ··· namespace Zend\Log\Writer;

use Zend\Log\AbstractWriter;

class StreamWriter extends AbstractWriter { } ···

While the next example displays proper usage: ··· namespace Zend\Log\Writer;

use Zend\Log\Writer;

abstract class AbstractWriter implements Writer { }

class StreamWriter extends AbstractWriter { } ···

The ·Abstract· prefix MAY be omitted from the class name in the case of abstract classes that implement only static functionality, such as factories. As an example, the following is valid usage: ··· namespace Zend\Uri;

abstract class UriFactory { public static function factory($uri) { // work goes in here... } } ···

Interfaces

Interfaces follow the same conventions as classes, with two additional rules: interface MUST be nouns or adjectives and interface class names SHOULD end with the term, ·Interface·. As examples, ·ServiceLocationInterface·, ·EventCollectionInterface·, and ·PluginLocatorInterface· are all considered appropriate interface names.

Interfaces MUST be in the same namespace as concrete implementations.

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