[ Team LiB ] Previous Section Next Section

PEAR and Your Own Code

If you are starting out on your path as a PHP programmer, it might be a little too soon to start talking about authoring your own PEAR package. Remember, though, that every package has its own maintainer and contributors, so there is plenty of opportunity to get involved. If you are interested, you should read the relevant PEAR documentation at http://pear.php.net/manual/en/developers.contributing.php.

Coding Standards

Whether you intend to contribute code to PEAR, you might be interested in the style guide to which contributors must adhere. The strictures might not all be to your taste, but there are some very good practices. The PEAR standards encourage programmers to use parentheses even where not strictly required, for example. So, leaving out the parentheses in an if statement that has only one execution line is perfectly legal, as shown here:


if ( $length=5 )
  endLine();

However, it is safer and more readable to use the parentheses:


if ( $length = 5 ) {
  endLine();
}

As you glance through your code, picking out control structures is much easier if they are formatted consistently.

The style guide also advocates the use of meaningful return values, even from functions that do not need to return a value to perform their task. A Boolean is a good choice here, returning true upon successful completion and false upon error.

One principle that is not in the guide is as follows: Where possible, you should always return the same data type from a function or method. This was impossible with PHP 4. PHP 4 PEAR classes often return a PEAR_Error object when a problem occurs. Over time, PEAR libraries will likely throw exceptions in these circumstances, allowing functions to return cleanly.

The guide recommends that you always use <?php ?> opening and closing tags. These are the only combinations that you can be absolutely certain will be available with every PHP installation you encounter. You should code defensively.

By convention, you will find that C and Java comments (// and /* */) are favored over Perl/Shell-style comments (#).

You can read the full PEAR style guide at http://pear.php.net/manual/en/standards.php.

    [ Team LiB ] Previous Section Next Section