CHAPTER 12 – PEAR STANDARDS – Indentation
PEAR uses four-character indentation, with spaces only (no tabs!). This part of the PEAR coding standards alone has caused more controversy than any other part, so it deserves some explanation. Users expect the tab key in their editor to do some form of indentation. This may range from simply inserting a tab character into the file, or some- thing clever like looking at the indentation of the previous line to figure out how to indent the current line. It does not have to insert a tab character into the source file. When someone views a source file with tab characters in it, it is up to the viewer program how they are rendered. Traditionally, from the old days of VT100 UNIX terminals and typewriters, tab characters were rendered by moving the cursor to the next multiple-of-eight column. The Emacs editor ren- ders tabs as up to eight spaces by default; most Windows and Macintosh edi- tors use four spaces. Most editors let you configure the tab width, which gives even more possibilities. The result is that if you put a tab character in a file, the reader of the file is likely to see different indentation than you intended, because his viewer program renders the tab differently from your editor.
There are many examples of this, but rest assured that the only reliable way of rendering a certain amount of whitespace at the beginning of a line is using only space characters. For more rant on this issue, look at http:// www.jwz.org/doc/tabs-vs-spaces.html. Here is an example that demonstrates the PEAR indentation style: <?php class IndentExample { static $tmpfiles = array(); function sampleMethod($dbh, $id) { $results = $dbh->getAll('SELECT * FROM bar WHERE id = ?', array($id)); if (PEAR::isError($results)) { return $results; } foreach ($results as $row) { switch ($row[0]) { case 'foo': print "A foo-style row<br />n"; break; case 'bar': print "A bar-style row<br />n"; break; default: print "Something else...<br />n"; break; } } } } function clean_up() { foreach (IndentExample::$tmpfiles as $tmpfile) { if (file_exists($tmpfile)) { unlink($tmpfile); } } IndentExample::$tmpfiles = array(); } ?>