This document provides the coding standards and guidelines for developers and teams working on or with the Zend Framework. The subjects covered are:
- PHP File Formatting
- Naming Conventions
- Coding Style
- Inline Documentation
Good coding standards are important in any development project, particularly when multiple developers are working on the same project. Having coding standards helps to ensure that the code is of high quality, has fewer bugs, and is easily maintained.
For files that contain only PHP code, the closing tag ("?>") is to be omitted. It is not required by PHP, and omitting it prevents trailing whitespace from being accidentally injected into the output.
IMPORTANT: Inclusion of arbitrary binary data as permitted by __HALT_COMPILER() is prohibited from any Zend framework PHP file or files derived from them. Use of this feature is only permitted for special installation scripts.
Use an indent of 4 spaces with no tab characters. Editors should be configured to treat tabs as spaces in order to prevent injection of tab characters into the source code.
The target line length is 80 characters; i.e., developers should aim keep code as close to the 80-column boundary as is practical. However, longer lines are acceptable. The maximum length of any line of PHP code is 120 characters.
Line termination is the standard way for Unix text files. Lines must end only with a linefeed (LF). Linefeeds are represented as ordinal 10, or hexadecimal 0x0A.
Do not use carriage returns (CR) like Macintosh computers (0x0D).
Do not use the carriage return/linefeed combination (CRLF) as Windows computers (0x0D, 0x0A).
Lines should not contain trailing spaces. In order to facilitate this convention, most editors can be configured to strip trailing spaces, such as upon a save operation.
The Zend Framework employs a class naming convention whereby the names of the classes directly map to the directories in which they are stored. The root level directory of the Zend Framework is the "Zend/" directory, under which all classes are stored hierarchically.
Class names may only contain alphanumeric characters. Numbers are permitted in class names but are discouraged. Underscores are only permitted in place of the path separator. For example, the filename "Zend/Db/Table.php" must map to the class name "Zend_Db_Table".
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 "Zend_PDF" is not allowed, while "Zend_Pdf" is acceptable.
Zend Framework classes that are authored by Zend or one of the participating partner companies and distributed with the Framework must always start with "Zend_" and must be stored under the "Zend/" directory hierarchy accordingly.
These are examples of acceptable names for classes:
IMPORTANT: Code that operates with the framework but is not part of the framework, such as code written by a framework end-user and not Zend or one of the framework's partner companies, must never start with "Zend_".
Interface classes must follow the same conventions as other classes (see above), but must end with "_Interface", such as in these examples:
For all other files, only alphanumeric characters, underscores, and the dash character ("-") are permitted. Spaces are prohibited.
Any file that contains any PHP code must end with the extension ".php". These examples show the acceptable filenames for containing the class names from the examples in the section above:
File names must follow the mapping to class names described above.
Function names may only contain alphanumeric characters. Underscores are not permitted. Numbers are permitted in function names but are discouraged.
Function names must always start with a lowercase letter. When a function name consists of more than one word, the first letter of each new word must be capitalized. This is commonly called the "studlyCaps" or "camelCaps" method.
Verbosity is encouraged. Function names should be as illustrative as is practical to enhance understanding.
These are examples of acceptable names for functions:
For object-oriented programming, accessors for object members should always be prefixed with either "get" or "set". When using design patterns, such as the Singleton or Factory patterns, the name of the method should contain the pattern name where practical to make the pattern more readily recognizable.
Functions in the global scope, or "floating functions," are permitted but discouraged. It is recommended that these functions be wrapped in a class and declared static.