Geeklog 1.x never had any formal coding guidelines so you may find different styles in different places. As of March 2007, all new code in Geeklog will have to follow the PEAR coding standards (see below for details).
To quote from the Geeklog 2 Coding Standards:
Code conventions are important to programmers for a number of reasons:
Using the PEAR coding standard makes sense for several reasons:
The following is about the actual source code formatting, i.e. how to place braces and what and how much whitespace to use. The PEAR naming conventions (file names, function names, etc.) are too different from Geeklog's and are not part of Geeklog's Coding Guidelines.
Specifically, we are adopting the following PEAR conventions:
To be defined: “Header Comment Blocks” (9). It may also make sense to adopt “E_STRICT-compatible code” (10) but that may be difficult to achieve in some contexts. It should certainly become a long-term goal, though.
Use an indent of 4 spaces, with no tabs. This helps to avoid problems with diffs, patches, CVS history and annotations.
For Emacs you should set indent-tabs-mode to nil. Here is an example mode hook that will set up Emacs (ensure that it is called when you are editing PHP files):
(defun php-mode-hook ()
(setq tab-width 4
c-basic-offset 4
c-hanging-comment-ender-p nil
indent-tabs-mode
(not
(and (string-match "/\\(PEAR\\|pear\\)/" (buffer-file-name))
(string-match "\.php$" (buffer-file-name))))))
Here are Vim rules for the same thing:
set expandtab set shiftwidth=4 set softtabstop=4 set tabstop=4
It is recommended to keep lines at approximately 75-85 characters long for better code readability.
These include if, for, while, switch, etc. Here is an example if statement, since it is the most complicated of them:
<?php
if ((condition1) || (condition2)) {
action1;
} elseif ((condition3) && (condition4)) {
action2;
} else {
defaultaction;
}
?>
Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls.
You are strongly encouraged to always use curly braces even in situations where they are technically optional. Having them increases readability and decreases the likelihood of logic errors being introduced when new lines are added.
For switch statements:
<?php
switch (condition) {
case 1:
action1;
break;
case 2:
action2;
break;
default:
defaultaction;
break;
}
?>
Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis, and the semicolon. Here's an example:
<?php $var = foo($bar, $baz, $quux); ?>
As displayed above, there should be one space on either side of an equals sign used to assign the return value of a function to a variable. In the case of a block of related assignments, more space may be inserted to promote readability:
<?php $short = foo($bar); $long_variable = foo($baz); ?>
Function declarations follow the “BSD/Allman style”:
<?php
function fooFunction($arg1, $arg2 = '')
{
if (condition) {
statement;
}
return $val;
}
?>
Arguments with default values go at the end of the argument list. Always attempt to return a meaningful value from a function if one is appropriate. Here is a slightly longer example:
<?php
function connect(&$dsn, $persistent = false)
{
if (is_array($dsn)) {
$dsninfo = &$dsn;
} else {
$dsninfo = DB::parseDSN($dsn);
}
if (!$dsninfo || !$dsninfo['phptype']) {
return $this->raiseError();
}
return true;
}
?>
Complete inline documentation comment blocks (docblocks) must be provided. Please read the Sample File and Header Comment Blocks (to be defined - Dirk) sections of the Coding Standards to learn the specifics of writing docblocks for PEAR packages. Further information can be found on the phpDocumentor website.
Non-documentation comments are strongly encouraged. A general rule of thumb is that if you look at a section of code and think “Wow, I don't want to try and describe that”, you need to comment it before you forget how it works.
C style comments (/* */) and standard C++ comments (//) are both fine. Use of Perl/shell style comments (#) is discouraged.
Anywhere you are unconditionally including a class file, use “require_once”. Anywhere you are conditionally including a class file (for example, factory methods), use “include_once”. Either of these will ensure that class files are included only once. They share the same file list, so you don't need to worry about mixing them - a file included with “require_once” will not be included again by “include_once”.
Note: “include_once” and “require_once” are statements, not functions. Parentheses should not surround the subject filename.
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.
All scripts contributed to Geeklog must: