Classes, Properties & Methods
In this lesson we will look at the styling rules for classes, properties and methods in PHP.
The term “class” refers to all classes, interfaces, and traits.
Extends and Implements
The extends
and implements
keywords MUST be declared on the same line as the class name.
The opening brace for the class MUST go on its own line; the closing brace for the class MUST go on the next line after the body.
<?phpnamespace Vendor\Package;use FooClass;use BarClass as Bar;use OtherVendor\OtherPackage\BazClass;class ClassName extends ParentClass implements \ArrayAccess, \Countable{// constants, properties, methods}
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.
<?phpnamespace Vendor\Package;use FooClass;use BarClass as Bar;use OtherVendor\OtherPackage\BazClass;class ClassName extends ParentClass implements\ArrayAccess,\Countable,\Serializable{// constants, properties, methods}
Properties
-
Visibility MUST be declared on all properties.
-
The var keyword MUST NOT be used to declare a property.
-
There MUST NOT be more than one property declared per statement.
-
Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility.
-
A property declaration looks like the following.
<?phpnamespace Vendor\Package;class ClassName{public $foo = null;}
Methods
-
Visibility MUST be declared on all methods.
-
Method names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility.
-
Method names MUST NOT be declared with a space after the method name. The opening brace MUST go on its own line, and the closing brace MUST go on the next line following the body. There MUST NOT be a space after the opening parenthesis, and there MUST NOT be a space before the closing parenthesis.
A method declaration looks like the following.
Note the placement of parentheses, commas, spaces, and braces:
<?phpnamespace Vendor\Package;class ClassName{public function fooBarBaz($arg1, &$arg2, $arg3 = []){// method body}}
Method Arguments
In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma.
Method arguments with default values MUST go at the end of the argument list.
<?phpnamespace Vendor\Package;class ClassName{public function foo($arg1, &$arg2, $arg3 = []){// method body}}
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.
When the argument list is split across multiple lines, the closing parenthesis and opening brace MUST be placed together on their own line with one space between them.
<?phpnamespace Vendor\Package;class ClassName{public function aVeryLongMethodName(ClassTypeHint $arg1,&$arg2,array $arg3 = []) {// method body}}