Closing Brace (No Comment or Statement on the Same Line)
-
Rule: Any closing brace (
}) that marks the end of a class, method, or control structure MUST NOT be followed by any comment or statement on the same line. -
Reason: Placing a comment or statement on the same line as the closing brace can reduce code readability and clarity. Keeping the closing brace on its own line makes the structure of the code clearer and helps in distinguishing the end of a block of code.
Example (Correct):
class MyClass {
public function myMethod() {
// method code
}
}
Example (Incorrect):
class MyClass {
public function myMethod() {
// method code
} // This brace closes a function
} // This is a comment about brace closing a class
Parentheses When Instantiating a Class
-
Rule: When instantiating a new class, parentheses MUST always be present even when no arguments are passed to the constructor.
-
Reason: Explicitly including parentheses when creating a new object ensures consistency and makes it clear that a constructor is being called. It also avoids ambiguity, especially for classes with overloaded constructors or future changes that may add arguments.
Example:
// Always include parentheses when instantiating a class
$foo = new Foo(); // Parentheses even if no arguments are passed
Summary:
-
Closing brace (
}) should not be followed by a comment or statement on the same line. -
Instantiating classes: Always include parentheses when creating a new object, even if there are no constructor arguments.