Table of contents
Coding Standard
The rules and recommendations set forth in this section apply to PHP, JavaScript, SQL and any other programming language (where applicable) in use by PunBB. Please note that tabs in this document have been substituted by spaces for visual reasons only.
1. Naming convention
The naming rules apply to the naming of variables, functions, classes, attributes, arrays, array elements, HTML form fields, query string parameters, database tables, database fields as well as any other applicable entities.
- All names should be all lowercase.
- Use _ (underscore) as word seperator. E.g. date_format. Never start a name with the underscore character.
- Use the prefix "num" for entities that represent a count. E.g. num_users. Use the prefix "cur" for entities that represent the current element when iterating through any type of collection (database result set, array etc).
- Avoid all forms of Hungarian notation or derivates or variations thereof.
- Use common sense. If in doubt, look at similar sections in the PunBB source code.
2. Brace policy and indentation
The indent style and brace policy of choice for the PunBB project is the Allman style. All indentation should be made with tabs, not spaces. Here's an example:
<?php if ($a == $b) { do_something(); } else { do_something_else(); } ?>
Note the whitespace between the keyword and the parenthesis. One allowed exception from the standard Allman style is "braceless" blocks. The use of braceless blocks is actually encouraged. For example:
<?php if ($a == $b) do_something(); else do_something_else(); ?>
If the author prefers it, the code block can also be placed on the same line as the control statement.
3. Line breaks
All line breaks should be LF only. Set your editor to save files with UNIX style line breaks.
4. PHP-specific
The following rules apply only to PHP.
- Use singlequotes as opposed to doublequotes when working with strings. E.g. $str = 'Users: '.$num_users; as opposed to $str = "Users: $num_users";.
- Leave one line of whitespace above and below each block of markup, provided the block constitutes multiple lines of markup. I.e. one empty line above each ?> and below each <?php.
5. SQL-specific
The following rules apply only to SQL.
- Always escape potentially harmful data using $db->escape(). Expected integer values should be forced into integer form using intval() before use in a query.
- Use the $db->query_build() method whenever possible.
- Prefix tables names and table indices with $db->prefix when using the $db->query() method.
- Use the SQL2003 style explicit join notation.
- Whenever possible, write cross-database compatible queries. Rely on helper functions in the database abstraction layer as much as possible (e.g. $db->field_exists(), $db->add_field() etc). If database specific queries are required, provide alternative queries for other supported databases.
