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

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."

T Theodoros Kafantaris December 06, 2025 1 min read
Asymmetric Visibility: The "Read-Only" Solution We Needed

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.

Share this post

Back to Blog

Let's talk

Stay Curious. Stay Inspired.

Join our community of thinkers, developers, and lifelong learners. Explore ideas that challenge and inspire you to think differently.