Skip to content

Code Style Guide: PHP

ginatrapani edited this page Sep 13, 2010 · 20 revisions

This is the Code Style guide for PHP. See the main Code Style Guide page for other languages.

Assume we’re using the Drupal PHP coding style unless otherwise noted here.

Always use <?php to delimit PHP code, not the shorthand, <?, as this is the most used and supported across PHP setups and the one PHP will continue supporting in the future.

Use PHP5 Conventions

When in doubt, use PHP5 (not PHP4) coding conventions.

  • Class constructors should be public function __construct(), not the PHP4-style class name. Use destructors when appropriate.
  • Explicitly declare visibility (public, private, protected) for member variables and methods.
  • Do NOT use PHP closing tags for files that contain only PHP code:

The ?> at the end of code files is purposely omitted. This includes for module and include files. … Removing it eliminates the possibility for unwanted whitespace at the end of files which can cause “header already sent” errors, XHTML/XML validation issues, and other problems.

Include Docblocks on All New Code

Thinktank uses PHPDocumentor to ease code maintenance and auto-generate class documentation. Include PHPDoc-style “docblocks” in all of your PHP code. When writing your documentation, please use PHPDocumentor’s syntax.

Keyword case

Drupal style guide states use of uppercase value keywords (TRUE, FALSE, NULL), ThinkTank user lowercase.

MVC Architecture

ThinkTank implements the Model-View-Controller design pattern. All new PHP code should follow suit. Read more about ThinkTank’s MVC implementation.

Clone this wiki locally