Keywords and Types

Verze:

17. 06. 2025

Zodpovědná osoba:

Dominik Šlechta

PHP Reserved Keywords and Types in Lowercase

  • Rule: All PHP reserved keywords and types MUST be written in lowercase.

  • Reason: Consistency and readability are improved when keywords and types are always written in lowercase. It follows the common PHP coding standards and makes it easier for developers to read the code.

Example:

// Correct: Reserved keywords and types in lowercase
if ($var instanceof array) {
    // Do something
}

Short Form of Type Keywords

  • Rule: The short form of type keywords MUST be used.

    • For example, use bool instead of boolean, int instead of integer, and so on.

  • Reason: Short form type keywords are more concise and are the standard in modern PHP codebases.

Example:

// Correct: Using short type keywords
function foo(bool $flag, int $num): bool {
    return $num > 0;
}

Typed Arguments and Return Types

  • Rule: Every function MUST have typed arguments and a return type.

  • Reason: Explicitly typing arguments and return types makes the code easier to understand and less error-prone. It helps with type safety and can prevent bugs.

  • Retrospective rule: This rule applies retrospectively depending on the version of the project. For modern versions of PHP (7.0 and above), strict typing should be applied, including type declarations for function arguments and return values.

Example:

// Correct: Function with typed arguments and return type
function processOrder(string $orderId, int $quantity): bool {
    return $quantity > 0;
}

Summary:

  • Keywords and types (e.g., if, int, bool) MUST be in lowercase.

  • Use the short form of type keywords (e.g., bool instead of boolean, int instead of integer).

  • Functions MUST have typed arguments and a return type for clarity and type safety.