Learning Joomla! 3 Extension Development(Third Edition)
上QQ阅读APP看书,第一时间看更新

Coding standards

The Joomla! coding standards are based on the PEAR coding standards with some small variations. You can see the full details on the developer site at http://developer.joomla.org/5-policies/3-Joomla-Coding-Standards.html.

The first thing to note is that Joomla! uses tabs for indenting and not spaces. The encoding on files must be set to UTF-8, as some other languages need this encoding to display all the different characters, and it is important that Joomla! supports multiple languages.

The closing PHP tag ?> at the end of each file should be left off; your web server is going to automatically handle this, and by leaving it off, you prevent accidental whitespace.

Joomla! uses the Allman style for braces, so the opening brace is on the next line.

if (x == y)
{
  //do something
}

This is unlike the PEAR coding standards which use the K&R style where the opening brace is on the first line.

if (x == y) {
  //do something
}

There are still some minor inconsistencies with the standards used in the core code, but this is improving with each version. It's always best to make your code as similar in style to the core code as possible, as it will make it easier for other people to customize your code to suit their purposes. There is nothing worse than wanting to make a minor change to an extension and finding that the developer's code looks nothing like what you are familiar with, so you waste a lot of time just trying to reverse engineer it. The core components that are mostly like third-party extensions are Weblinks (com_weblinks) and Banners (com_banners); it's a good idea to use these as a reference point. It should be noted that any IDE worth its salt can reformat the code to suit the operator's preference, so minor formatting problems are only really an issue if you are using a basic text editor.