Skip to content

Coding style

Přemysl Karbula edited this page Jun 7, 2016 · 1 revision

PHP

  • Indent with tabs.

  • Opening curly braces { on the same line, closing } has a standalone new line.

  • One empty line amongst functions, methods, etc.

  • We are modern, array definitions use square brackets (almost every time; occasionaly the word array can be recognized better with when using a code highlighter)

  • We are also pretty. Sometimes it is good (aesthetic and well-arranged) to put empty lines among some lines of code.

  • With multi-line definitions of array it is good to put a comma even after the last item (it's easier to add new items).

  • Letter case:

  • Variables use camelCase:

$someAwesomeVariable = '...';

  • Constants use capital letters and underscores:

LESS_AWESOME_CONSTANT = '...';

  • Methods (and in most cases functions, too) use camelCase:
protected function doAwesomeThings($isEverythingAwesome = self::DEFAULT_AWESOMENESS) {
    ...
}
  • Classes + Namespaces (class, interface, trait, ...) use PascalCase:
namespace CoolModule;

class TotallyAwesomeManager extends BaseManager implements IManager {
    ...
}

Example (of pretty decent code):

/**
 * Definitions of all existing notifications.
 * ...
 */
class Notifications extends \Nette\Object {

	// ...

	/**
	 * @var array
	 *
	 * Default configuration of sending (for
	 * now only e-mail) user notifications.
	 */
	protected static $defaultConfig = [
		self::PRIORITY_SYSTEM => self::FREQ_PROMPTLY,
		self::PRIORITY_HIGH => self::FREQ_PROMPTLY,
		self::PRIORITY_MEDIUM => self::FREQ_PROMPTLY,
		self::PRIORITY_LOW => self::FREQ_PROMPTLY,
	];

	// Getters

	/**
	 * Obtain a list of all notifications having desired priority.
	 *
	 * @param string Notification priority
	 * @return array All notifications of desired priority.
	 */
	public static function getNotificationsByPriority($priority) {

		static $cache = array();
		if (isset($cache[$priority])) return $cache[$priority];

		foreach (self::$notifications as $n) {
			if ($n[self::PRIORITY] == $priority) $cache[$priority][] = $n;
		}

		return $cache[$priority];

	}

	// ...

}
Clone this wiki locally