abstract, final, and static

Verze:

18. 06. 2025

Zodpovědná osoba:

Dominik Šlechta

Order of abstract and final Declarations

  • Rule: When present, the abstract and final declarations MUST precede the visibility declaration (e.g., public, protected, private).

  • Reason: This ensures a consistent order of modifiers, making the code more readable and easier to follow. The abstract and final keywords describe the behavior of the method or class before specifying visibility.

Example (Correct):

abstract class MyClass {
    abstract public function myMethod();
}

final class MyFinalClass {
    public function myMethod() {
        // Method body
    }
}

Example (Incorrect):

abstract class MyClass {
    public abstract function myMethod();
}

Order of static Declaration

  • Rule: When present, the static declaration MUST come after the visibility declaration.

  • Reason: The static keyword describes how the method or property behaves (i.e., whether it belongs to the class rather than instances of the class), so it should follow the visibility declaration to maintain a consistent order of modifiers.

Example (Correct):

class MyClass {
    public static function myMethod() {
        // Static method body
    }

    private static $myStaticProperty;
}

Example (Incorrect):

class MyClass {
    static public function myMethod() {
        // Static method body
    }
}

Summary:

  • abstract and final declarations MUST precede the visibility declaration.

  • static declarations MUST come after the visibility declaration.