// *** before: ***
class Order {
private string $status = 'draft';
public function getStatus(): string {
return $this->status;
}
public function markPaid(): void {
$this->status = 'paid';
}
// a plain getter is the only way to expose a read-only view
}
// *** in version 8.4: ***
class Order {
public private(set) string $status = 'draft';
// readable from anywhere, but settable only from inside the class
public function markPaid(): void {
$this->status = 'paid';
}
}
$order = new Order();
echo $order->status; // "draft", reading works
// $order->status = 'paid'; // Error: cannot modify private(set) property