Asymmetric Visibility: The "Read-Only" Solution We Needed

Asymmetric Visibility: The "Read-Only" Solution We Needed

T

Theodoros Kafantaris

Δημοσιεύτηκε στις December 06, 2025

One of the most common requirements in Object-Oriented Programming is: "I want everyone to be able to read this status, but only the class itself should be able to change it."

Previously, we had to make the property private and create a public getter. Now, PHP supports Asymmetric Visibility.

The Old Way:

Code
                    class Order {
    private string $status = 'pending';

    public function getStatus(): string {
        return $this->status;
    }
}
                

The Modern Way:

Code
                    class Order {
    // Public to read, Private to set.
    public private(set) string $status = 'pending';

    public function complete(): void {
        $this->status = 'completed'; // This works (internal)
    }
}

$order = new Order();
echo $order->status; // Works!
$order->status = 'shipped'; // Error! Cannot set property.
                

Why use it? It is perfect for DTOs (Data Transfer Objects) and Entities. You get the safety of encapsulation with the simplicity of public properties.

Μοιραστείτε το άρθρο

Προκαλέστε το Μυαλό Σας

NEW!

Κάντε ένα διάλειμμα από το διάβασμα και δοκιμάστε τις ικανότητές σας στη λογική με τον ημερήσιο γρίφο μας!

Τελευταία Πρόκληση: Jul 9, 2026

Daily Logic Ladder - July 9, 2026

Παίξτε τον Σημερινό Γρίφο

Σχετικά με το Blog Μας

Explore where technology meets intellect. From technical tutorials to intellectual exploration—stay curious and inspired.

Ⓒ 2026. Με επιφύλαξη παντός δικαιώματος atomic