| Defined | src/docs/contributing/php_coding_standards.diviner:1 |
|---|---|
| Group | Contributing |
This document describes PHP coding standards for Phabricator and related projects (like Arcanist and libphutil).
This document outlines technical and style guidelines which are followed in libphutil. Contributors should also follow these guidelines. Many of these guidelines are automatically enforced by lint.
These guidelines are essentially identical to the Facebook guidelines, since I basically copy-pasted them. If you are already familiar with the Facebook guidelines, you probably don't need to read this super thoroughly.
if/else:
if ($some_variable > 3) { // ... } else if ($some_variable === null) { // ... } else { // ... }
You should always put braces around the body of an if clause, even if it is only one line long. Note spaces around operators and after control statements. Do not use the "endif" construct, and write "else if" as two words.
for:
for ($ii = 0; $ii < 10; $ii++) { // ... }
Prefer $ii, $jj, $kk, etc., as iterators, since they're easier to pick out visually and react better to "Find Next..." in editors.
foreach:
foreach ($map as $key => $value) { // ... }
switch:
switch ($value) { case 1: // ... break; case 2: if ($flag) { // ... break; } break; default: // ... break; }
break statements should be indented to block level.
array literals:
$junk = array( 'nuts', 'bolts', 'refuse', );
Use a trailing comma and put the closing parenthesis on a separate line so that diffs which add elements to the array affect only one line.
operators:
$a + $b; // Put spaces around operators. $omg.$lol; // Exception: no spaces around string concatenation. $arr[] = $element; // Couple [] with the array when appending. $obj = new Thing(); // Always use parens.
function/method calls:
// One line eject($cargo); // Multiline AbstractFireFactoryFactoryEngine::promulgateConflagrationInstance( $fuel, $ignition_source);
function/method definitions:
function example_function($base_value, $additional_value) { return $base_value + $additional_value; } class C { public static function promulgateConflagrationInstance( IFuel $fuel, IgnitionSource $source) { // ... } }
class:
class Dog extends Animal { const CIRCLES_REQUIRED_TO_LIE_DOWN = 3; private $favoriteFood = 'dirt'; public function getFavoriteFood() { return $this->favoriteFood; } }