PHP Coding Standards
From Wikipedia of the Dark Jedi Brotherhood, an online Star Wars Club
|Dark Brotherhood Coding Standards|
|Coding Standards and Practices|
|ASP Coding Standards||CSS Coding Standards||Database Standards|
- All associative arrays should use single quotes around element names.
- All global variables should be referred to using the
- Super global variables should be used as opposed to relying on register_globals being on
- A given include should not be done repeatedly in the same script.
- A closing PHP tag (
?>) at the end of the file is extraneous and should be removed to prevent unexpected whitespace output. See: Zend File Formatting Standards
- Hard coded paths should be avoided whenever possible.
- Function and method names should be all lowercase, with underscores separating words:
- Any operation that is being done repeatedly in the application should be functionalized. Functions or Object-Oriented PHP should be used whenever possible.
- Passwords that appear in PHP code must be base64 encoded, then decoded when utilized.
- API classes should be written in preference to functions in the global scope.
- When possible, write code in distinct, testable chunks. (See Unit Testing below.)
- Global variables should be all uppercase and use
$GLOBALSwhere appropriate. Ex.
- Local variables should NEVER be in all uppercase.
The following global variables should be defined for every script:
||This is the base directory for the application and should not be hard coded if possible. Used by PSUSmarty and PSUTemplate to create a unique template cache directory.|| /var/htdocs/myapp/subdir
||This is the base URL for the application and should be a full relative path.||/webapp/subdir/|
||Full path to your application's templates directory. Required if using DJBTemplate.||
||Application title, as displayed in templates by DJBTemplate. Required if using DJBTemplate.||"APE"|
- All files that include XHTML and conditional PHP must be separated into a .html and a .tpl file using Smarty. Conditional PHP and HTML code should never exist in the same file.
- Exception: high traffic content may follow other rendering guidelines due to performance.
- Database abstraction with ADOdb should be used when connecting to databases.
- Database connections should be opened using the registry, i.e. tools::db('djb').
- Registry database shortcuts should be defined centrally in the tools class.