Translations of this page: en bg cs de fi fr hu it ja pl ru tr zh

This is an old revision of the document!


Coding standards

Recommendations below describes the appearance of PHP, JavaScript, SQL or any other code used in PunBB and extensions.

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 the word separator. E.g. date_format. Never start a name with the underscore character (except for nested functions, which should start with an 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 derivatives or variations thereof.
  • Use common sense. If in doubt, look at similar sections in the PunBB source code.

Formatting

All line breaks should be LF only. Set your editor to save files with UNIX style line breaks.

The indent style and brace policy is the Allman style. All indentation should be made with tabs, not spaces. Examples:

if (<expression>)
{
    <statement>;
    ...
    <statement>;
}
else
{
    <statement>;
    ...
    <statement>;
}
 
while (<expression)
{
    <statement>;
    ...
    <statement>;
}

One allowed exception from the standard Allman style is “braceless” blocks:

if (<expression>)
    <statement>;
else
    <statement>;
 
while (<expression)
    <statement>;

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. The exception to this is when there is only one line of code, in which case the entire section of code should be on one line.
  • Don't use PHP's short tags (<?, <?=), but use its full equivalent (<?php, <?php echo).
  • Don't use $_REQUEST. Use $_POST or $_GET instead.
  • Define the FORUM_ROOT constant to the relative path to the PunBB root directory, if you want to include common.php or essentials.php.
  • Try to avoid PHP warnings/notices in any cases.

SQL

  • Always escape potentially harmful data using
    $forum_db->escape()

    Expected integer values should be forced into integer form using intval() before use in a query.

  • Prefix tables names and table indexes with
    $forum_db->prefix
  • Use the SQL2003 style explicit join notation.
  • Whenever possible, write cross-database compatible queries. Developers should take advantage of database helpers like
    $forum_db->query_build()

    . They allows to easily create/extend cross-database compatible queries. In rare cases where database specific queries are needed, every effort should be made to provide alternative queries for other supported databases.

CSS

Here is the example of the indentation in CSS files:

.class {
    attribute: value;
    }

Personal Tools