-
Notifications
You must be signed in to change notification settings - Fork 114
Dev.Coding Standard PHP
Based on Zend Framework 2 Coding Standards.
#PHP File Formatting
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 MUST consist of 4 spaces. Tabs MUST NOT be used for indentation.
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 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.
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 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, whileZend\Pdf
is acceptable. - the namespace
Zend\XMLRPC
would not be allowed, whileZend\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.
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
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 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 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.