PHP Coding Standards: Difference between revisions

From Wikipedia of the Dark Brotherhood, an online Star Wars Club
m (Place holder until Orv gets t this page, getting it off the wanted pages list :P)
 
m (re-adding from cache)
Line 1: Line 1:
PHP Coding Standards
<center>
<table border="1">
<tr><th colspan="3"> [[Dark Brotherhood]] Coding Standards
</th></tr>
<tr><td colspan="3" align="center"> [[Coding Standards and Practices]]
</td></tr>
<tr>
<td> [[ASP Coding Standards]]
</td>
<td> [[CSS Coding Standards]]
 
</td>
<td> [[Database Standards]]
</td>
</tr>
<tr>
<td> [[HTML Coding Standards]]
</td>
<td> [[JavaScript Coding Standards]]
</td>
<td> [[PHP Coding Standards]]
</td>
</tr>
</table>
</center>
 
 
== General ==
* All associative arrays should use single quotes around element names.
* All global variables should be referred to using the <code>$GLOBALS['VAR_NAME']</code> syntax.
* 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 (<code>?></code>) at the end of the file is extraneous and should be removed to prevent unexpected whitespace output. See: [http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html Zend File Formatting Standards]
* Hard coded paths should be avoided whenever possible.
* Function and method names should be all lowercase, with underscores separating words: <code>my_function()</code> rather than <code>myFunction()</code>.
* 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 [http://us3.php.net/base64-encode 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 ==
* Global variables should be all uppercase and use <code>$GLOBALS</code> where appropriate. Ex. <code>$GLOBALS['MY_GLOBAL_VAR']</code>
 
* Local variables should NEVER be in all uppercase.
The following global variables should be defined for every script:
 
<table class="grid">
<tr>
<th> Variable
</th>
<th> Purpose
</th>
<th> Example
</th>
</tr>
<tr>
<td> <code>$GLOBALS['BASE_DIR']</code>
 
</td>
<td> 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.
</td>
<td> <tt>/var/htdocs/myapp/subdir</tt>
 
<code>dirname(__FILE__)</code>
</td>
</tr>
<tr>
<td> <code>$GLOBALS['BASE_URL']</code>
</td>
<td> This is the base URL for the application and should be a full relative path.
</td>
<td> <tt>/webapp/subdir/</tt>
</td>
</tr>
<tr>
<td> <code>$GLOBALS['TEMPLATES']</code>
</td>
<td> Full path to your application's templates directory. Required if using DJBTemplate.
</td>
<td> <code>$GLOBALS['BASE_DIR'] . '/templates';</code>
</td>
</tr>
<tr>
<td> <code>$GLOBALS['TITLE']</code>
</td>
<td> Application title, as displayed in templates by DJBTemplate. Required if using DJBTemplate.
 
</td>
<td> "APE"
</td>
</tr>
</table>
== Templating ==
*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 ==
*Database abstraction with ADOdb should be used when connecting to databases.
*Database connections should be opened using the registry, ie. tools::db('djb').
*Registry database shortcuts should be defined centrally in the tools class.
 


[[Category: DJB Info]]
[[Category: DJB Info]]
[[Category:Coding Standards]]
[[Category:Coding Standards]]

Revision as of 07:07, 29 May 2011

Dark Brotherhood Coding Standards
Coding Standards and Practices
ASP Coding Standards CSS Coding Standards Database Standards
HTML Coding Standards JavaScript Coding Standards PHP Coding Standards


General

  • All associative arrays should use single quotes around element names.
  • All global variables should be referred to using the $GLOBALS['VAR_NAME'] syntax.
  • 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: my_function() rather than myFunction().
  • 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

  • Global variables should be all uppercase and use $GLOBALS where appropriate. Ex. $GLOBALS['MY_GLOBAL_VAR']
  • Local variables should NEVER be in all uppercase.

The following global variables should be defined for every script:

Variable Purpose Example
$GLOBALS['BASE_DIR'] 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

dirname(__FILE__)

$GLOBALS['BASE_URL'] This is the base URL for the application and should be a full relative path. /webapp/subdir/
$GLOBALS['TEMPLATES'] Full path to your application's templates directory. Required if using DJBTemplate. $GLOBALS['BASE_DIR'] . '/templates';
$GLOBALS['TITLE'] Application title, as displayed in templates by DJBTemplate. Required if using DJBTemplate. "APE"

Templating

  • 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

  • Database abstraction with ADOdb should be used when connecting to databases.
  • Database connections should be opened using the registry, ie. tools::db('djb').
  • Registry database shortcuts should be defined centrally in the tools class.