CHAPTER 12 – PEAR STANDARDS

PEAR's Coding Standard, or PCS for short, is primarily meant for develop- ers of PEAR packages. Some of it is useful for those who just use PEAR pack- ages as well, especially the section about how different types of symbols are named. Even if you are not planning to develop any PEAR packages yourself, it is a good idea to read the section on naming so you know what to expect when you use PEAR packages.

Symbol Naming Different types of symbols, such as function or variable names, have naming schemes designed to make each type of symbol stand out from each other.

Constants Constant names are all uppercase, with the (upper- cased) package name as a prefix. Here are some examples: PEAR_ERROR_DIE (from PEAR package) AUTH_EXPIRED (from Auth package, without namespaces) DB_DATAOBJECT_INT (from DB_DataObject package)

Optionally, if you do not care about PHP 4 compatibility, use class const variables. With class const variables, you must use the properly capitalized class name, and then the constant name in all uppercase: PEAR_Error::DIE (from PEAR package) Auth::EXPIRED (from Auth package, without namespaces) DB_DataObject::INT (from DB_DataObject package)

Global Variables With the advent of static class variables in PHP 5, there is little reason to use global variables in library code anymore. Packages that are PHP 4-compatible cannot use static class variables, of course. Here is PEAR's naming convention for globals: $_Package_Name_variable The convention is $_{Package_Name}_{lowercased_variable_name}. The low- ercasing is for clearly separating the package name part (which requires an initial capital letter in each underscore-separated element), and the variable name part.

Functions Functions are named simply with the package name pre- fixed as for constants. The package name has its case preserved; the part fol- lowing the prefix is studlyCaps with an initial lowercase letter. Here is an example: function Package_Name_functionName() { print "Röyksopp<br />n"; } If the function is "private," which means that it is not intended for use outside the package that defines it, the name is prefixed with an underscore: function _Package_Name_privateFunction() { print "Dadafon<br />n"; }

Post Comment
Login to post comments