Home » Releases » PHP 7.4 »

PHP 7.4 Code Comparisons

Explore PHP 7.4

Typed Properties

PHP 7.4 introduced native type declarations for class properties, bringing strict type safety to object state without relying on unenforced PHPDoc annotations (@var) or verbose getter/setter boilerplate. Typed properties enforce type bounds upon assignment. Assigning an incompatible value throws a TypeError. Properties declared with a type start in an uninitialized state; accessing an uninitialized property before assigning a value results in an Error. Supported types include scalar types (int, string, bool, float), arrays, objects, iterables, self, parent, class/interface names, and nullable variants (?Type).
Before PHP 7.4
class User
{
    / @var int */
    public $id;

    /** @var string */
    public $name;

    /** @var string|null */
    public $email;

    public function __construct(
        int $id,
        string $name,
        ?string $email = null
    ) {
        $this->id = $id;
        $this->name = $name;
        $this->email = $email;
    }
}

$user = new User(1, 'Alice');
PHP 7.4 or Later
class User
{
    public int $id;
    public string $name;
    public ?string $email;

    public function __construct(
        int $id,
        string $name,
        ?string $email = null
    ) {
        $this->id =$id;
        $this->name =$name;
        $this->email =$email;
    }
}

$user = new User(1, 'Alice');

Arrow Functions

Arrow functions offer a shorthand syntax for writing simple anonymous functions. Unlike traditional closures, arrow functions automatically capture variables from the outer scope by value, eliminating the need for explicit use ($var) declarations. They consist of a single expression whose result is implicitly returned. They support full parameter and return type declarations, reference passing, and variadics, but cannot contain multiple statements or a manual return keyword.
Before PHP 7.4
$factor = 10;
$numbers = [1, 2, 3, 4, 5];

$multiplied = array_map(
    function (int $n) use ($factor): int {
        return $n * $factor;
    },
    $numbers
);
PHP 7.4 or Later
$factor = 10;
$numbers = [1, 2, 3, 4, 5];

$multiplied = array_map(
    fn(int $n): int => $n * $factor,
    $numbers
);

Null Coalescing Assignment Operator

The null coalescing assignment operator (??=) combines the null coalescing operator (??) with variable assignment into a single step. If the variable or array key on the left side is unset or evaluates to null, the right-hand operand is evaluated and assigned to it. If the left side is already set and non-null, the right side is skipped entirely without evaluation.
Before PHP 7.4
$user = ['role' => 'editor'];

// Assign default if key is missing or null
$user['role'] = $user['role'] ?? 'guest';
$user['status'] = $user['status'] ?? 'active';

$config = [];
if (!isset($config['timeout'])) {
    $config['timeout'] = 30;
}
PHP 7.4 or Later
$user = ['role' => 'editor'];

// Assign default if key is missing or null
$user['role'] ??= 'guest';
$user['status'] ??= 'active';

$config = [];
$config['timeout'] ??= 30;

Array Unpacking in Array Expressions

The spread operator (...) allows unpacking traversable objects and arrays directly inside array literals. This provides a cleaner and often more performant alternative to functions like array_merge(). It allows positional elements, function return values, and multiple array sources to be combined inline effortlessly.
Before PHP 7.4
$fruits = ['apple', 'banana'];$vegetables = ['carrot', 'pea'];

$groceries = array_merge(
    ['bread'],
    $fruits, $vegetables,
    ['milk']
);
PHP 7.4 or Later
$fruits = ['apple', 'banana'];
$vegetables = ['carrot', 'pea'];

$groceries = [
    'bread',
    ...$fruits,
    ...$vegetables,
    'milk',
];