Published
2. Asymmetric Property Visibility: Finer Control Over Class Properties.
Before PHP 8.4, if a property was public, anyone could both read and modify it. If you wanted a property to be publicly readable but privately modifiable, you had to make it private and expose a public getter method. PHP 8.4 introduces asymmetric visibility, allowing you to set separate access levels for reading and writing a property.
PHP 8.4 Example
PHP
class Order
{
// Publicly readable, but only writable within the class
public private(set) string $status = 'pending';
public function complete(): void
{
$this->status = 'completed';
}
}
$order = new Order();
echo $order->status; // Works: 'pending'
// $order->status = 'canceled'; // Throws Fatal Error: Cannot modify private(set) propertyThe articles and images featured on this platform were generated with the assistance of AI tools.