An effective PHP coding style, based on the PSR-1. It can be used as an alternative to the PSR-2.
Current version: 1.0
- Brazilian Portuguese SOON!
PHP-FIG's PSR-1 presents a good basic coding standard for PHP applications and does not impose a style. However, the PSR-2 coding style guide has some inconvenient guidelines such as:
- It recommends the use of spaces instead of TABs;
- Opening braces start in a new line instead of starting in the same line;
- Opening parentheses for control structures do not have a pharentheses after them.
- etc.
Hence the coding style presented here can be used as an alternative to PSR-2. It is simpler, presents some code examples and have a different structure. We point out the differences between the guidelines during their definition, so take a look and make your conclusions.
Are you in a hurry? Jump to the examples.
The following piece of code is the same presented in PSR-2 (here), but uses our coding style.
<?php
namespace Vendor\Package;
use FooInterface;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;
class Foo extends Bar implements FooInterface {
public function sampleFunction( $a, $b = null ) {
if ( $a === $b ) {
bar();
} else if ( $a > $b ) {
$foo->bar( $arg1 );
} else {
BazClass::bar( $arg2, $arg3 );
}
}
public static final function bar() {
// method body
}
}
?>
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.
- Code MUST follow the PSR-1 "coding style guide"; (same as PSR-2)
- Directories MUST use dashed-case (examples:
api
,sample-code
); (not defined in PSR-2) - PHP files MUST be named with the
.php
extension. You MAY use.class.php
or.inc.php
but this is NOT RECOMMENDED; (not defined in PSR-2) - Files that contain classes MUST be named using the main class' name (example:
MyImportantClass.php
); - Files that do not containt classes MUST be named in dashed-case (example:
hello-world.php
)
- Files MAY use any line-ending (Windows, Unix or Mac). However, all project files SHOULD use the same line-ending; (different from PSR-2)
- There MUST NOT be a hard limit on line length; the soft limit MUST be
120
characters; lines SHOULD be80
characters or less; (same as PSR-2)
- Code MUST use tabs for indenting, not spaces. Tab size MUST be equivalent to
4
spaces; (different from PSR-2)
- PHP files MUST use only UTF-8 without Byte Order Mark (BOM); (same as PSR-2)
- There MUST be one blank line after the declaration of:
namespace
s; (same as PSR-2)use
s; (same as PSR-2)class
es; (different from PSR-2)- between methods or functions; (not defined in PSR-2)
- There MUST be one blank space:
- After control structures; (same as PSR-2)
- After opening parenthesis; (different from PSR-2)
- Before closing parenthesis; (different from PSR-2)
- Before and after the following operators: (not defined in PSR-2)
1. Arithmetic operators, except for negation (e.g.
-$x
); 2. Assignment operators; 3. Bitwise operators; 4. Comparison operators; 5. Logical operators; 6. String operators; 7. Array operators; 8. Type operators; - After comma; (not defined in PSR-2)
- Methods and function calls MUST NOT have one blank space after them; (same as PSR-2)
- Opening braces MUST go on the same line, and closing braces MUST go on the next line after the body, for any code constructions; (different from PSR-2)
- PHP keywords MUST be in lower case; (same as PSR-2)
- The PHP constants
true
,false
, andnull
MUST be in lower case; (same as PSR-2) else
MUST be separated fromif
onelse if
s; (different from PSR-2)abstract
,final
andstatic
MUST be declared after the visibility, andstatic
MUST be declared afterfinal
; (different from PSR-2)include
,require
,include_once
, andrequire_once
MUST not use parentheses (i.e.:require_once 'MyClass.php';
);
- Visibility MAY be declared on properties and methods (PHP assumes
public
as the default visibility); (different from PSR-2) - Constants MUST be declared in all upper case with underscore separators. Example:
MAX_ENERGY
. (same as PSR-2) - Classes, interfaces and traits MUST be declared using PascalCase. Example:
MySimpleClass
. (same as PSR-2) - Methods MUST be declared using camelCase. Example:
mySimpleMethod
. (same as PSR-2) - Closures MUST be declared with a space after the
function
keyword, and a space before and after theuse
keyword. (same as PSR-2) - Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line. (same as PSR-2)
- The extends and implements keywords MUST be declared on the same line as the class name. (same as PSR-2)
- Lists of implements MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one interface per line. (same as PSR-2)
- Control structures MUST always use braces; (not defined in PSR-2)
- Classes without a namespace MUST be referenced with a backslash (
\
) before their names. Example:\MyClass
. (not defined in PSR-2) - The PHPDoc style MAY be used for documenting files, namespaces, classes, interfaces, traits, methods, functions, and other code constructions.
Some elements were not included in this guide, and may appear in a next version:
- namespaces;
- some good practices;
Do you want to improve it? Please open an Issue.
See some examples here.