Visibility Declaration
-
Visibility (e.g.,
public,protected, `private) MUST be declared on all methods. -
Reason: Declaring visibility makes it clear how methods can be accessed (internally or externally). It helps improve code clarity and maintainability.
Example:
class MyClass {
public function myMethod() {
// Method body
}
protected function myProtectedMethod() {
// Method body
}
private function myPrivateMethod() {
// Method body
}
}
No Underscore Prefix for Method Names
-
Method names MUST NOT be prefixed with a single underscore to indicate protected or private visibility.
-
Reason: The underscore prefix has no special meaning for visibility. Visibility should be indicated by the visibility modifier (
public,protected,private), not by naming conventions.
Example (Correct):
class MyClass {
private function myMethod() {
// Method body
}
}
Example (Incorrect):
class MyClass {
private function _myMethod() {
// Method body
}
}
No Space After Method Name
-
Method and function names MUST NOT be declared with a space after the method name.
-
Reason: This follows modern PHP conventions and helps maintain consistency with PHP syntax, as no space should be added between the method name and parentheses..
Example (Correct):
class MyClass {
public function myMethod() {
// Method body
}
}
Example (Incorrect):
class MyClass {
public function myMethod () {
// Method body
}
}
Opening Brace Placement
-
The opening brace (
{) for the method MUST go on its own line. -
Exception: If no return type is specified, the opening brace MUST go on the same line as the method declaration.
-
Reason: This enhances readability by clearly marking the beginning of the method body. If the return type is not declared, the opening brace is typically placed on the same line for consistency with shorter method signatures.
Example:
class MyClass {
public function myMethod()
{
// Method body
}
}
Closing Brace Placement
-
The closing brace (
}) for the method MUST go on the next line following the body of the method. -
Reason: This improves the readability of the code and helps distinguish the end of the method from the next part of the class.
Example (Correct):
class MyClass {
public function myMethod() {
// Method body
}
}
Example (Incorrect):
class MyClass {
public function myMethod() {
// Method body }
}
Summary:
-
Visibility MUST be declared on all methods.
-
Method names MUST NOT be prefixed with a single underscore to indicate protected or private visibility.
-
Method and function names MUST NOT have a space after the method name.
-
The opening brace MUST go on its own line (unless no return type is specified, in which case it goes on the same line).
-
The closing brace MUST go on the next line after the body of the method.