-
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