Asymmetric Visibility (PHP 8.4)

Ma Mahalakshmi V Updated 14 Nov 2026
5 min read ·Lesson 188 of 255

What Is Asymmetric Visibility?

In simple terms: Often, everyone should be able to read a property, but only the class itself should be able to change it. An order's status, for example, should be visible everywhere, but it should change only through methods like pay() and ship() that enforce the rules. Asymmetric visibility lets you declare exactly that: public to read, private to write.

Technically: Since PHP 8.4, a property can have separate visibility for reading and writing. The write ("set") visibility is written in parentheses: public private(set) string $status. The first modifier controls reading; private(set) or protected(set) controls writing. The set visibility may not be wider than the read visibility.

The Problem It Solves

Before PHP 8.4Drawback
Private property + public getter methodBoilerplate: a getter for every readable property
public readonly propertyCan be set only once, so the class itself cannot update it later
Public propertyAnyone can change it to anything

With asymmetric visibility, the property is readable like a public property, changeable as often as the class needs, and protected from outside modification.


Syntax

public private(set) string $status;      // read: public,  write: private
public protected(set) int $count;        // read: public,  write: this class and subclasses
protected private(set) array $cache;     // read: protected, write: private
private(set) string $name;               // shorthand: read is public

A First Example: An Order Status

<?php
final class Order
{
    public private(set) string $status = 'pending';
    public private(set) array $history = [];

    public function __construct(public readonly int $id) {}

    public function pay(): void
    {
        $this->transition('pending', 'paid');
    }

    public function ship(): void
    {
        $this->transition('paid', 'shipped');
    }

    private function transition(string $from, string $to): void
    {
        if ($this->status !== $from) {
            throw new LogicException("Order {$this->id} cannot go from {$this->status} to $to.");
        }

        $this->history[] = "{$this->status} → $to";
        $this->status = $to;
    }
}

$order = new Order(501);
echo "Status: {$order->status}\n";

$order->pay();
$order->ship();

echo "Status: {$order->status}\n";
echo implode(', ', $order->history) . "\n";

$attempts = [
    'assign status' => fn() => $order->status = 'delivered',
    'append history' => fn() => $order->history[] = 'hacked',
];

foreach ($attempts as $label => $attempt) {
    try {
        $attempt();
    } catch (Error $e) {
        echo "$label: " . $e->getMessage() . "\n";
    }
}

Output

Status: pending
Status: shipped
pending → paid, paid → shipped
assign status: Cannot modify private(set) property Order::$status from global scope
append history: Cannot modify private(set) property Order::$history from global scope

How It Works

  1. public private(set) makes status and history readable everywhere — $order->status works in templates and API responses without getter methods.
  2. Only code inside Order may write them. The class changes the status repeatedly (pending → paid → shipped), which a readonly property could not do.
  3. The only way to change the status is through methods that enforce the allowed transitions and record the history.
  4. Outside writes are rejected — including indirect modifications such as appending to an array property.

protected(set): Writable by Subclasses

<?php
abstract class Counter
{
    public protected(set) int $count = 0;

    abstract public function record(): void;
}

final class DownloadCounter extends Counter
{
    public function record(): void
    {
        $this->count++;   // allowed: subclass
    }
}

$downloads = new DownloadCounter();
$downloads->record();
$downloads->record();
$downloads->record();

echo "Downloads: {$downloads->count}\n";

try {
    $downloads->count = 1000;
} catch (Error $e) {
    echo $e->getMessage() . "\n";
}

Output

Downloads: 3
Cannot modify protected(set) property Counter::$count from global scope

protected(set) lets child classes update the value while still preventing outside code from doing so. The error message names Counter, the class that declares the property.


Constructor Promotion

Asymmetric visibility works with promoted constructor parameters:

<?php
final class Customer
{
    public function __construct(
        public private(set) string $name,
        public private(set) string $email,
    ) {}

    public function changeEmail(string $email): void
    {
        if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
            throw new InvalidArgumentException("Invalid email: $email");
        }

        $this->email = strtolower($email);
    }
}

$customer = new Customer('Asha', 'asha@example.com');
$customer->changeEmail('Asha.K@GoCourse.in');

echo "{$customer->name} <{$customer->email}>\n";

Output

Asha <asha.k@gocourse.in>

Callers read $customer->email directly, but changing it must go through changeEmail(), which validates the new address.


Combining With Property Hooks

Asymmetric visibility controls who may write; property hooks control what happens on write. Together they cover many cases elegantly:

public private(set) string $sku {
    set => strtoupper(trim($value));
}

The SKU is publicly readable, writable only inside the class, and always normalized when written (see Property Hooks (PHP 8.4)).


Asymmetric Visibility vs Readonly

public readonlypublic private(set)
Readable from outsideYesYes
Writable from outsideNoNo
Can the class change it later?No — initialized onceYes, as often as needed
Can be combined with property hooksNoYes
Typical useImmutable values (IDs, value objects)State that changes through controlled methods (status, counters)

Since PHP 8.4, readonly properties are implicitly protected(set), which lets child classes initialize them; they still can only be initialized once.


Rules

RuleDetail
The property must have a typeUntyped properties cannot use asymmetric visibility
Set visibility cannot be wider than read visibilityprivate public(set) is invalid
Shorthandprivate(set) string $x means public private(set)
Static propertiesSupported for static properties from PHP 8.5
private(set) implies finalSubclasses cannot redeclare a private(set) property, because they could not write it anyway

Common Mistakes

Using readonly for Values the Class Must Update

Effect: Cannot modify readonly property when the class tries to change the value later.

Fix: Use public private(set).

Expecting Arrays to Be Modifiable From Outside

Effect: Appending or changing elements from outside throws an error.

Fix: Provide methods such as addItem().

Exposing Writes Through Methods Without Checks

Effect: A setStatus(string $s) method that accepts anything defeats the purpose.

Fix: Write intention-revealing methods (pay(), ship()) that enforce the rules.

Forgetting the Type

Effect: Compile error.

Fix: Declare a type for every property with asymmetric visibility.


Best Practices

  • Use public private(set) for state that others may read but only the object may change.
  • Use protected(set) when subclasses must update the value.
  • Change such properties only through methods that express business actions and enforce rules.
  • Prefer public readonly for values that never change after construction.
  • Combine asymmetric visibility with set hooks for normalization and validation.

Official reference: https://www.php.net/manual/en/language.oop5.visibility.php#language.oop5.visibility-members-aviz

0 Comments

Reviewed before they appear

No comments yet.

PHP
PHP Home PHP Intro PHP Install PHP Built-in Web Server PHP Syntax PHP Tags PHP and HTML PHP Comments PHP Multiline Comments PHP Variables Variables Scope Static Variables Variable Variables PHP Echo / Print PHP printf / sprintf PHP Data Types PHP Booleans PHP NULL PHP Strings String Functions Modify Strings Concatenate Strings Slicing Strings Escape Characters String Interpolation Heredoc and Nowdoc Search in Strings Multibyte Strings PHP Floats PHP Casting PHP Type Juggling PHP Math PHP Random Numbers PHP Constants PHP Magic Constants PHP Operators Arithmetic Operators Assignment Operators Comparison Operators Increment / Decrement Operators Logical Operators Null Coalescing Operator Spaceship Operator Operator Precedence PHP If...Else...Elseif PHP If PHP If Operators PHP If...Else PHP Shorthand if PHP Nested if PHP Switch PHP Match Alternative Syntax PHP Loops While Loop Do While Loop For Loop Foreach Loop Nested Loops Break Statement Continue Statement PHP Functions User-defined Functions Function Arguments Default Argument Values Pass by Reference Named Arguments Variadic Functions Return Values Type Declarations in Functions Recursive Functions Variable Functions Anonymous Functions Closures Arrow Functions First-class Callable Syntax PHP Arrays Indexed Arrays Associative Arrays Create Arrays Access Array Items Update Array Items Add Array Items Remove Array Items Sorting Arrays Multidimensional Arrays Array Destructuring Array Spread Operator Search in Arrays Merge Arrays array_map / array_filter / array_reduce Array Functions PHP Superglobals $GLOBALS $_SERVER $_REQUEST $_POST $_GET $_FILES $_COOKIE $_SESSION \$\_ENV PHP RegEx RegEx Patterns RegEx Modifiers preg_match preg_replace preg_split PHP RegEx Functions PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail Form Input Sanitizing Escaping Output (htmlspecialchars) Checkbox, Radio and Select Inputs Sticky Forms PHP Form Complete PHP Date and Time Date Formatting DateTime Class Timezones PHP Include Include vs Require PHP File Handling PHP File Open/Read PHP File Create/Write File Delete / Rename / Copy PHP Directories PHP CSV Files PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP Callback Functions PHP JSON PHP Exceptions Error Handling Custom Error Handler Error Logging HTTP Headers and Redirects Output Buffering Sending Email PHP cURL Consuming REST APIs PHP Generators PHP What is OOP PHP Classes/Objects The $this Keyword PHP Properties PHP Methods PHP Constructor Constructor Property Promotion PHP Destructor PHP Access Modifiers Readonly Properties PHP Inheritance Method Overriding The final Keyword PHP Class Constants PHP Abstract Classes PHP Interfaces Polymorphism PHP Traits PHP Static Methods PHP Static Properties Late Static Binding Magic Methods Object Cloning Comparing Objects Anonymous Classes PHP Enums PHP Namespaces Autoloading Classes PHP Iterables Iterators Object Serialization Strict Types Union Types Nullable Types Mixed and Never Types Nullsafe Operator PHP Attributes PHP Fibers Readonly Classes Property Hooks (PHP 8.4) Asymmetric Visibility (PHP 8.4) Reflection API SPL Data Structures MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data MySQL Joins with PHP Pagination with PHP PDO Introduction PDO Connect PDO Prepared Statements PDO Fetch Modes PDO Transactions PDO Error Handling PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat Parser PHP DOM Parser PHP XMLReader PHP XMLWriter AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll AJAX with JSON Security Introduction Password Hashing SQL Injection Prevention XSS Prevention CSRF Protection Session Security Secure Cookies File Upload Security Filesystem Security Error Reporting in Production Hiding PHP Encryption with Sodium PHP Configuration (php.ini) PHP Command Line (CLI) Composer PSR-4 Autoloading Installing Extensions (PECL / PIE) Debugging PHP OPcache & Performance Garbage Collection PHP-FPM Migrating to PHP 8 Project: Contact Form with Email Project: Login & Registration System Project: CRUD App with MySQL Project: Image Upload Gallery Project: Shopping Cart with Sessions Project: Simple Blog Project: REST API with PHP
Ask about this post
AI Ask about this post

Ask questions about Asymmetric Visibility (PHP 8.4) and get answers drawn from it.

Signed-in readers only.