PHP 8 introduced and enhanced several features related to strict typing, also known as “strict mode.” This feature enforces stricter type checks during function calls. Here’s an overview of how it works:

1. Strict Types Declaration

  • PHP allows you to enable strict typing on a per-file basis by declaring strict_types=1 at the beginning of the file. When strict types are enabled, PHP will enforce strict type checking for function calls and return types.
  Copy code
<?php
declare(strict_types=1);

function add(int $a, int $b): int {
    return $a + $b;
}

echo add(1, 2); // 3
echo add(1.5, 2.5); // TypeError: Argument 1 passed to add() must be of the type int, float given
  

2. Behavior of Strict Mode

  • With strict_types=1: PHP requires the exact data type specified in the function signature. If you pass a float where an integer is expected, a TypeError will be thrown.
  • Without strict_types=1 (Default): PHP will attempt to coerce the type, converting a float to an integer, for example.
  // Without strict_types=1
function add(int $a, int $b): int {
    return $a + $b;
}

echo add(1.5, 2.5); // 3
  

3. Return Type Declarations

  • PHP 8 allows you to specify the return type of a function. In strict mode, the returned value must match the declared return type exactly.
  function multiply(float $a, float $b): float {
    return $a * $b;
}

echo multiply(2.5, 4.2); // 10.5
  

4. TypeError Exceptions

  • If a function is called with arguments that do not match the declared types, or if a function returns a value that does not match the declared return type, a TypeError exception will be thrown.

5. Nullable Types

  • PHP 8 supports nullable types, which allow null to be passed to a function or returned from it, even if the type is otherwise strict.
  function getName(?string $name): ?string {
    return $name;
}

echo getName(null); // null
  

6. Union Types

  • PHP 8 introduced union types, allowing a parameter or return type to accept more than one type.
  function processInput(int|string $input): int|string {
    if (is_int($input)) {
        return $input * 2;
    } else {
        return strtoupper($input);
    }
}

echo processInput(5); // 10
echo processInput("hello"); // HELLO