This topic provides guidelines for code formatting and documentation to individuals and teams. Especially in team project coding standing plays a vital role. Because once we follow the coding standard, it will be consistent and easy to understand by other developers. And it make the code with high quality.
PHP File Formatting:
General:
For files that contain only php code, closing tag ( ?> ) is not required by php, while omitting, it prevents the accidental injection of trailing white space into the response.
Indendation:
Indendation should consists only 4 spaces. Tabs are not allowed.
Line Length:
Minimum line length target by zend framework is 80 whereas maximum is 120.
Naming Conventions:
Classes:
- The names of the classes directly map to the directories in which they are stored.
- Class names may only contain alphanumeric characters.Numbers are permitted in class names but are discouraged in most cases.
- Underscores are only permitted in place of the path separator; 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.
Abstract Classes:
- In general, abstract classes follow the same conventions as classes, with one additional rule: abstract class names must end in the term, “Abstract”, and that term must not be preceded by an underscore.
- As an example,
Invalid Name: Zend_Controller_Plugin_Abstract
Valid Name: Zend_Controller_PluginAbstract (or)
Zend_Controller_Plugin_PluginAbstract.
Interfaces:
- In general, interfaces follow the same conventions as classes, with one additional rule: interface names may optionally end in the term, “Interface”, but that term must not be preceded by an underscore.
- As an example,
InValid Name: Zend_Controller_Plugin_Interface
Valid Name: Zend_Controller_PluginInterface (or)
Zend_Controller_Plugin_PluginInterface.
File Names:
- Only alphanumeric characters, underscores, and the dash character (“-“) are permitted.
- Spaces are strictly prohibited.
- Any file that contains PHP code should end with the extension “.php“. Eg: db.php
Functions and Methods:
- Function names may only contain alphanumeric characters.
- Underscores are not permitted.
- Numbers are permitted in function names but are discouraged in most cases.
- 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 “camelCase” formatting.
- Verbosity is generally encouraged. Function names should be as verbose as is practical to fully describe their purpose and behavior.
Variables:
- Variable names may only contain alphanumeric characters.
- Underscores are not permitted.
- Numbers are permitted in variable names but are discouraged in most cases.
- For instance variables that are declared with the “private” or “protected” modifier, the first character of the variable name must be a single underscore. This is the only acceptable application of an underscore in a variable name. Member variables declared “public” should never start with an underscore.
- Verbosity is generally encouraged.
- If a loop contains more than 20 lines of code, the index variables should have more descriptive names.
Constants:
- Constants may contain both alphanumeric characters and underscores.
- Numbers are permitted in constant names.
- All letters used in a constant name must be capitalized, while all words in a constant name 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 with the “const” modifier. Defining constants in the global scope with the “define” function is permitted but strongly discouraged.