Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^56) CHAPTER 6 ■ DOCUMENTATION AND CODING CONVENTIONS
Others prefer to place the brace on the next line:
function foo()
{
}
The Zend Framework and PEAR standards both call for the latter form, but they use the
former form when working with control structures. For instance, you’ll find code in the Zend
Framework that looks like this:
if($x == 1) {
$x += 50;
} else if ($x == 2) {
$x += 55;
} else {
$x = 60;
}
In PEAR packages, you’ll find an almost identical coding style:
if($x == 1) {
$x += 50;
} elseif ($x == 2) {
$x += 55;
} else {
$x = 60;
}
Pretty close, right? The only difference between the Zend and PEAR styles is that the elseif
isn’t spaced to else if. But neither follows the function-bracing standards just mentioned.
Others may say that the correct format is to always use the brace-on-a-new-line approach, like this:
if($x == 1)
{
$x += 50;
}
elseif ($x == 2)
{
$x += 55;
}
else
{
$x = 60;
}
Who’s right? Technically, no one.
If you are trying to define the standards for a new application, the easiest way by far is to
pick a project whose standards you like and follow them to the letter. For more information about
the Zend and PEAR standards, see the complete references at http://framework.zend.com/ and
http://pear.php.net/, respectively.
McArthur_819-9C06.fm Page 56 Friday, February 22, 2008 8:59 AM

Free download pdf