|Temporary Location for Coding Standards Review|
The coding standards official location can be found in the Zend Framework Online Manual and are copied here for review and updates before being placed back into DocBook format in Subversion. The manual also contains translated versions that are not available here.
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
- Errors and Exceptions
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.
Abstract goals we strive for:
- extreme simplicity
- tool friendliness, such as use of method signatures, constants, and patterns that support IDE tools and auto-completion of method, class, and constant names.
When considering the goals above, each situation requires an examination of the circumstances and balancing of various trade-offs.
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.
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.
- File Docblock
- Class Docblock
- Member Docblock
When creating an API for use by application developers (as opposed to Zend Framework internal developers), if application developers must identify abstractions using a compound name, separate the names using underscores, not camelCase. For example, the name used for the MySQL PDO driver is 'pdo_mysql', not 'pdoMysql'. When the developer uses a string, normalize it to lowercase. Where reasonable, add constants to support this (e.g. PDO_MYSQL).
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:
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 "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.
Though function names may not contain the underscore character, class methods that are declared as protected or private must begin with a single underscore, as in the following example:
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.
Functions or variables declared with a "static" scope in a class generally should not be "private", but protected instead. Use "final" if the function should not be extended.
Use "null" as the default value instead of "false", for situations like this:
public function foo($required, $optional = null)
when $optional does not have or need a particular default value.
However, if an optional parameter is boolean, and its logical default value should be true, or false, then using true or false is acceptable.
Variable names may only contain alphanumeric characters. Underscores are not permitted. Numbers are permitted in variable names but are discouraged.
For class member variables that are declared with the private or protected construct, the first character of the variable name must be a single underscore. This is the only acceptable usage of an underscore in a variable name. Member variables declared as "public" may never start with an underscore. For example:
Like function names, variable names must always start with a lowercase letter and follow the "camelCaps" capitalization convention.
Verbosity is encouraged. Variable names should always be as verbose as practical. Terse variable names such as "$i" and "$n" are discouraged for anything other than the smallest loop contexts. If a loop contains more than 20 lines of code, variables for such indices or counters need to have more descriptive names.
Constants may contain both alphanumeric characters and the underscore. Numbers are permitted in constant names.
Constant names must always have all letters capitalized.
To enhance readability, words in constant names must be separated by underscore characters. For example, "EMBED_SUPPRESS_EMBED_EXCEPTION" is permitted but "EMBED_SUPPRESSEMBEDEXCEPTION" is not.
Constants must be defined as class members by using the "const" construct. Defining constants in the global scope with "define" is permitted but discouraged.
Unlike PHP's documentation, the Zend Framework uses lowercase for both boolean values and the "null" value.