Coding Conventions and Guidelines
From PRADO Wiki
[edit]
Directory and File Organization
- Decision to create new directories must be reviewed by program managers.
- Each PHP class should be written in a file whose name is the same as the class name. The file can also contain classes that are only used by the main class.
[edit]
Naming Conventions
- Class names must start with the letter 'T' followed by a name in camel case (e.g. TTextBox). To avoid name conflict within the framework, choose wisely the class names. Avoid using names that are too generic, such as TParameter.
- Interface names must start with the letter 'I' followed by a name in camel case (e.g. IPostBackControl).
- Property names follows the camel case convention, i.e., words are joined without spaces, and each word is capitalized within the compound. For example, BorderWidth and FontName are all valid, while border_width and fontname are deprecated.
- Event names also follows the camel case convention with the additional requirement that the first word be On. For example, OnClick, OnPageIndexChanged are all valid.
- Function and variable names are similar to property names, except that the first word is not capitalized (e.g. authenticateUser(), $wordCount).
- Private class member names, please prefix them with an underscore character so that they can be differentiated easily from the other inheritable variables.
- Constant names should be all capitalized and the words in them be concatenated by underlines (e.g. TEXT_ALIGNMENT)
- Exception message strings should be in the format of classname_noun_reason, where classname refers to the class name (without 'T') that the exception is raised in, noun describes the subject related with the error, and reason is usually an adjective describing the error. Characters in messages must be in lower-case. For example, control_id_invalid is recommended.
[edit]
Recommended Practices
- Avoid defining global variables and constants. Instead, define static classes and class constants.
- Avoid using properties directly. Use the corresponding getter and setter methods. For example, $this->getText() is recommended, while $this->Text is not.
- Use Prado::using(Path.To.Class) to include class files, rather than include_once() or require_once().
- When modifying an existing file, try to follow the existing coding style so that the file looks consistent in format.
- Use private class member variables instead of protected or public. Write accessor methods to give access to the member variables.
[edit]
PHP Tricks You Should Know
- === is better than == as the latter will do type conversion
- $var===null is better than is_null($var)
- $str!== is better than strlen($str)
- ++$i is better than $i++
- for($i=0, $k=count($j); $i<$k; ++$i) is better than for($i=0; $i<count($j); ++$i)
- ctype_digit($foo) is better than preg_match("![0-9]+!", $foo)
- if(isset($array['key'])) is better than if(in_array('key', $keys))
- if(!isset($foo{5})) is better than if(strlen($foo) < 5)
- echo is better than print
- true is better than TRUE
- false is better than FALSE
- null is better than NULL
More details can be found in PHP Optimization Tricks and Optimizing PHP Scripts.

