| 5.2. Coding Guidelines |
| Prev | Chapter 5. Developer Guidelines | Next |
|---|
All code must work with PHP register_globals = Off.
Always use <?php ?>
to delimit PHP code, not the <? ?> shorthand.
This is required for PEAR compliance and is also the most portable way
to include PHP code on differing operating systems and setups.
Use single quotes to refer to an index between brackets of an array (ex: $foo['name'] and not $foo[name] or $foo["name"])
Use single quotes instead of double quotes as much as possible because it's faster to parse.
Indent using 4 spaces or a tab.
To ease long term readability of source code, the text and tags must conform to the order and spacing provided in the example above. This standard is adopted from the JavaDoc standard.
Include SVN $Header$ strings in your code. This makes it easier for people to know which version of a file they have and where it came from, so that they can usefully refer to the file’s SVN history to find out about bugs and fixes, etc. If your repository is configured appropriately, use the custom tag instead of $Header$.
Always run Mambo/Joomla! and PHP will full Error Reporting Level (E_ALL). You can change this level in the global configuration (see „Server“ ⇒ Error Reporting Level) and in your PHP.ini.
Always initialize variables. (just $a=0 is initialization)
Use isset( $var ) to check if a variable has been set. Use empty( $var ) to check if Array indexes have been set or are empty.
All source code files in the repository shall contain a "page-level" docblock at the top of each file and a "class-level" docblock immediately above each class. Below are examples of such docblocks.
<?php
/**
* Short description for file
*
* Long description for file (if any)...
*
*
* @package VirtueMart
* @subpackage classes_product
* @author Original Author <author@example.com>
* @author Another Author <another@example.com>
* @copyright 2007 VirtueMart Developer Team
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @version $Id: Developer_Manual.xml 1500 2008-08-06 03:31:16Z soeren_nb $
*/
/*
* Place includes, constant defines and $_GLOBAL settings here.
* Make sure they have appropriate docblocks to avoid phpDocumentor
* construing they are documented by the page-level docblock.
*/
/**
* Short description for class
*
* Long description for class (if any)...
*
* @author Original Author <author@example.com>
* @author Another Author <another@example.com>
* @copyright 2004-2007 VirtueMart Developer Team
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @version Release:
*/
class foo {
/** @var database Internal database class pointer */
var $_db=null;
/** @var object An object of configuration variables */
var $_config=null;
/** @var object An object of path variables */
var $_path=null;
/** @var mosSession The current session */
var $_session=null;
/** @var string The current template */
var $_template=null;
/** @var array An array to hold global user state within a session */
/**
* This function does something special.
* @since VirtueMart 1.0.1
* @param string The name of the product
* @param int The ID of the product
* @return string HTML Table with a "snapshot" of the product
*/
function myFunction( $arg1, &$arg2 ) {
}
}
?>Short Descriptions
Short descriptions must be provided for all docblocks. They should be a quick sentence, not the name of the item, but the description of the „what does this file / class?“.
@license
VirtueMart is released under the GNU/GPL license. You should keep this license for best compatibility.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
@author
There's no hard rule to determine when a new code contributor should be added to the list of authors for a given source file. In general, their changes should fall into the "substantial" category (meaning somewhere around 10% to 20% of code changes). Exceptions could be made for rewriting functions or contributing new logic.
Simple code reorganization or bug fixes would not justify the addition of a new individual to the list of authors.
@copyright
Feel free to apply whatever copyrights you desire. When formatting this tag, the year should be in four digit format and if a span of years is involved, use a hyphen between the earliest and latest year. The copyright holder can be you, a list of people, a company, the PHP Group, etc. Examples:
* @copyright 2003 John Doe and Jennifer Buck * @copyright 2001-2004 John Doe * @copyright 2005 XYZ Corporation