PHP / Magento Dev Blog

  • Publikacje
  • O autorze
  • Kontakt

PHP 7.4 preview – typed properties, arrow functions, spread in arrays, preloading

by Henryk Tews / Tuesday, 10 September 2019 / Published in PHP

PHP 7.4 arrives in November 2019 and brings several changes that will have a real impact on everyday code. Typed properties are the headline feature – at last we can declare types directly on class properties. Plus arrow functions, spread in arrays, and the null coalescing assignment operator.

Typed Properties – finally

Before PHP 7.4 you enforced property types through the constructor and docblocks. From 7.4 you declare the type directly on the property:

<?php

declare(strict_types=1);

class Product
{
    // PHP 7.4 - type directly on the property
    private int $id;
    private string $name;
    private float $price;
    private ?string $description = null; // nullable with default
    private \DateTimeImmutable $createdAt;

    public function __construct(int $id, string $name, float $price)
    {
        $this->id        = $id;
        $this->name      = $name;
        $this->price     = $price;
        $this->createdAt = new \DateTimeImmutable();
    }
}

Properties without a default value are in an “uninitialized” state until assigned. Reading such a property throws an Error, not null:

<?php

class Broken
{
    public int $count; // no default value
}

$obj = new Broken();
echo $obj->count; // Error: Typed property must not be accessed before initialization

Arrow Functions – shorter anonymous function syntax

Arrow functions (fn() =>) automatically capture variables from the surrounding scope by value – no use keyword needed:

<?php

$multiplier = 3;

// Before PHP 7.4 - use required
$multiply = function(int $n) use ($multiplier): int {
    return $n * $multiplier;
};

// PHP 7.4 - automatic capture by value
$multiply = fn(int $n): int => $n * $multiplier;

echo $multiply(5); // 15

// Works great with array operations
$prices = [29.99, 9.99, 49.99, 14.99];
$discounted = array_map(fn(float $p): float => $p * 0.9, $prices);

$taxRate = 0.23;
$inStock = array_filter($products, fn(array $p): bool => $p['stock'] > 0);
$withTax = array_map(
    fn(array $p): array => array_merge($p, ['tax' => $p['stock'] * $taxRate]),
    $inStock
);

Spread operator in arrays

<?php

$defaults  = ['color' => 'red', 'size' => 'M', 'in_stock' => true];
$overrides = ['size' => 'XL', 'price' => 29.99];

// PHP 7.4 - spread in array literal
$product = [...$defaults, ...$overrides];
// ['color' => 'red', 'size' => 'XL', 'in_stock' => true, 'price' => 29.99]

// Useful when merging module config
$baseConfig   = ['timeout' => 30, 'retries' => 3, 'debug' => false];
$moduleConfig = ['debug' => true, 'endpoint' => 'https://api.example.com'];
$finalConfig  = [...$baseConfig, ...$moduleConfig];

Null coalescing assignment operator

<?php

// Before PHP 7.4
$data['key'] = $data['key'] ?? 'default';

// PHP 7.4 - shorthand
$data['key'] ??= 'default';

// Practical: lazy cache initialisation
class ConfigCache
{
    private array $cache = [];

    public function get(string $key, callable $loader): mixed
    {
        $this->cache[$key] ??= $loader();
        return $this->cache[$key];
    }
}

Deprecations in PHP 7.4

  • array_key_exists() on objects – use property_exists()
  • Nested ternary expressions without brackets – PHP now requires explicit parenthesising
  • Curly brace string offset access: use $str[0] instead of $str{0}

Summary

PHP 7.4 is a solid update – typed properties clean up class code, arrow functions shorten collection operation expressions, preloading opens performance optimisation possibilities. If you care about code readability and static analysis, typed properties is a change worth backporting to existing classes.

About Henryk Tews

What you can read next

PHP 7.2 – object type hint, sodium instead of mcrypt, deprecations

© 2026 Created by

TOP
Zarządzaj zgodą
Aby zapewnić jak najlepsze wrażenia, korzystamy z technologii, takich jak pliki cookie, do przechowywania i/lub uzyskiwania dostępu do informacji o urządzeniu. Zgoda na te technologie pozwoli nam przetwarzać dane, takie jak zachowanie podczas przeglądania lub unikalne identyfikatory na tej stronie. Brak wyrażenia zgody lub wycofanie zgody może niekorzystnie wpłynąć na niektóre cechy i funkcje.
Funkcjonalne Always active
Przechowywanie lub dostęp do danych technicznych jest ściśle konieczny do uzasadnionego celu umożliwienia korzystania z konkretnej usługi wyraźnie żądanej przez subskrybenta lub użytkownika, lub wyłącznie w celu przeprowadzenia transmisji komunikatu przez sieć łączności elektronicznej.
Preferencje
Przechowywanie lub dostęp techniczny jest niezbędny do uzasadnionego celu przechowywania preferencji, o które nie prosi subskrybent lub użytkownik.
Statystyka
Przechowywanie techniczne lub dostęp, który jest używany wyłącznie do celów statystycznych. Przechowywanie techniczne lub dostęp, który jest używany wyłącznie do anonimowych celów statystycznych. Bez wezwania do sądu, dobrowolnego podporządkowania się dostawcy usług internetowych lub dodatkowych zapisów od strony trzeciej, informacje przechowywane lub pobierane wyłącznie w tym celu zwykle nie mogą być wykorzystywane do identyfikacji użytkownika.
Marketing
Przechowywanie lub dostęp techniczny jest wymagany do tworzenia profili użytkowników w celu wysyłania reklam lub śledzenia użytkownika na stronie internetowej lub na kilku stronach internetowych w podobnych celach marketingowych.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Zobacz preferencje
  • {title}
  • {title}
  • {title}