Constants

Verze:

18. 06. 2025

Zodpovědná osoba:

Dominik Šlechta

Visibility Declaration (PHP 7.1+)

  • Rule: Visibility (e.g., public, protected, private) MUST be declared on all constants if your project PHP minimum version supports constant visibilities (PHP 7.1 or later).

  • Reason: Explicit visibility for constants provides better clarity on how they can be accessed within the class or outside of it.

Example:

class MyClass {
    public const MY_CONSTANT = 1;
    private const MY_SECRET = 'hidden';
}

Type Declaration (PHP 8.3+)

  • Rule: Type MUST be declared on all constants if your project PHP minimum version supports constant types (PHP 8.3 or later).

  • Reason: Explicit type declarations for constants provide stronger type safety and prevent unintended type issues.

Example (Correct):

class MyClass {
    public const PI: float = 3.14159;
    private const MAX_LENGTH: int = 255;
}

Example (Incorrect):

class MyClass {
    public const PI = 3.14159;
}

Summary:

  • Visibility MUST be declared on all constants (PHP 7.1+).

  • Type MUST be declared on all constants (PHP 8.3+).