New in PHP 8.4: Asymmetric Visibility cover image

New in PHP 8.4: Asymmetric Visibility

Scott Keck-Warren • January 12, 2025

One of the new features added to PHP 8.4 is the ability for us to define Asymmetric Visibility on properties inside our classes. This is done by defining the get visibility for the property as we do normally which will become the "read" visibility and then defining the set visibility using the new syntax. As you can see below this just involves adding protected or private and then (set).

<?php
class User
{
    public protected(set) string $hashedPassword;
}

$user = new User();
// Uncaught Error: Cannot modify protected(set) property
$user->hashedPassword = password_hash("CorrectHorseBatteryStaple", PASSWORD_DEFAULT);

Want to Know More?

I did a whole article for php[achitect] on property hooks where I got into more details. It's also available in a video version if you're so inclined.