PHP / Magento Dev Blog

  • Home

PHP 8.4 – property hooks, asymmetric visibility and new array functions

by Henryk Tews / Wednesday, 30 September 2026 / Published in PHP

PHP 8.4 has been available since November 2024. It brings property hooks – a native implementation of getters and setters without boilerplate, asymmetric property visibility (public private(set)), new array functions and deprecation of functions untouched since PHP 4. This evolution lets you write more concise code without giving up encapsulation.

Property hooks – native getters and setters

<?php

declare(strict_types=1);

// PHP 8.4: property hooks - logic directly on the property
class OrderItem84
{
    public float $price {
        set(float $value) {
            if ($value < 0) throw new \InvalidArgumentException('Price cannot be negative');
            $this->price = $value;
        }
        get => $this->price;
    }

    public int $qty {
        set(int $value) {
            if ($value < 1) throw new \InvalidArgumentException('Qty must be positive');
            $this->qty = $value;
        }
    }

    // get-only hook - virtual computed property
    public float $total {
        get => $this->price * $this->qty;
    }
}

$item = new OrderItem84();
$item->price = 99.99;  // triggers set hook
$item->qty   = 3;
echo $item->total;     // triggers get hook: 299.97
// $item->total = 100; // Fatal error: property with only get is readonly

Asymmetric property visibility

<?php

declare(strict_types=1);

// PHP 8.4: public property that only the class can modify
class Product
{
    public private(set) string $sku;
    public private(set) float  $price;
    public protected(set) int  $stock;  // only class and children can set

    public function __construct(string $sku, float $price, int $stock)
    {
        $this->sku   = $sku;
        $this->price = $price;
        $this->stock = $stock;
    }

    public function applyDiscount(float $percent): void
    {
        $this->price = $this->price * (1 - $percent / 100); // OK - inside class
    }
}

$product = new Product('SKU-001', 100.0, 50);

echo $product->sku;   // OK - public read
echo $product->price; // OK - public read

$product->price = 50; // Fatal error: Cannot modify private(set) property

$product->applyDiscount(10); // OK - inside class
echo $product->price;  // 90.0

New array functions – array_find() and friends

<?php

declare(strict_types=1);

$orders = [
    ['id' => 1, 'status' => 'complete',   'total' => 150.0],
    ['id' => 2, 'status' => 'pending',    'total' => 89.99],
    ['id' => 3, 'status' => 'processing', 'total' => 230.0],
    ['id' => 4, 'status' => 'pending',    'total' => 45.0],
];

// PHP 8.4: array_find() - first element satisfying condition
$firstPending = array_find($orders, fn($o) => $o['status'] === 'pending');
// ['id' => 2, 'status' => 'pending', 'total' => 89.99]

// PHP 8.4: array_find_key() - key of first matching element
$key = array_find_key($orders, fn($o) => $o['total'] > 200);
// 2 (index of order with total 230)

// PHP 8.4: array_any() - whether any element satisfies condition
$hasPending = array_any($orders, fn($o) => $o['status'] === 'pending');
// true

// PHP 8.4: array_all() - whether all elements satisfy condition
$allComplete = array_all($orders, fn($o) => $o['status'] === 'complete');
// false

Deprecations and breaking changes

<?php

// PHP 8.4 deprecates implicit nullable - you must be explicit
// PHP 8.3 (worked without warning):
function oldStyle(string $name = null): void {}

// PHP 8.4 (deprecated - throws E_DEPRECATED):
// function oldStyle(string $name = null): void {}

// PHP 8.4 (correct):
function newStyle(?string $name = null): void {}
// or:
function newStyle2(string|null $name = null): void {}

// Scan your code with:
// vendor/bin/phpstan analyse --level=6 app/code/
// or rector:
// vendor/bin/rector process app/code/ --config rector.php

Summary

PHP 8.4 property hooks eliminate the need to write getters and setters while preserving full encapsulation. Asymmetric visibility (public private(set)) allows properties that are publicly readable but privately writable – without extra code. New array functions fill the gap previously closed with array_filter and foreach. Before upgrading check for implicit nullable – it is the most common issue when migrating.

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}