Most classes have a few properties that the rest of the code needs to read but should never change. For years the answer was a private property plus a public getter. It works, but it's a lot of typing for "look, don't touch".
PHP 8.4 added asymmetric visibility: a property can have one visibility for reading and a tighter one for writing. PHP 8.5 extended it to static properties.
The syntax in one minute
You write the read visibility first, then the write visibility in brackets:
public private(set) int $total;: anyone reads, only this class writes.public protected(set) int $total;: anyone reads, this class and subclasses write.private(set) int $total;: shorthand forpublic private(set).
It works with constructor property promotion too, so public private(set) int $totalPence can sit straight in the constructor.
A worked example: an invoice that moves through its life
An invoice has a number that never changes and a status that does change, but only through the right steps.
```php <?php declare(strict_types=1);
enum OrderStatus: string { case Draft = 'draft'; case Sent = 'sent'; case Paid = 'paid'; }
final class Invoice { public private(set) OrderStatus $status = OrderStatus::Draft; public private(set) ?DateTimeImmutable $paidAt = null;
public function __construct( public readonly string $number, public private(set) int $totalPence, ) {}
public function send(): void { if ($this->status !== OrderStatus::Draft) { throw new LogicException("Invoice {$this->number} has already been sent."); } $this->status = OrderStatus::Sent; }
public function markPaid(DateTimeImmutable $when): void { if ($this->status !== OrderStatus::Sent) { throw new LogicException("Invoice {$this->number} must be sent before it is paid."); } $this->status = OrderStatus::Paid; $this->paidAt = $when; } }
$invoice = new Invoice('INV-0107', 12_500); $invoice->send(); $invoice->markPaid(new DateTimeImmutable('2026-11-10 09:30'));
echo $invoice->status->value, PHP_EOL; // paid echo $invoice->paidAt?->format('Y-m-d'), PHP_EOL; // 2026-11-10
try { $invoice->status = OrderStatus::Draft; } catch (Error $e) { echo $e->getMessage(), PHP_EOL; } ```
The last block prints "Cannot modify private(set) property Invoice::$status from global scope". Controllers, templates and reports can all read $invoice->status directly. The only way to change it is send() or markPaid(), and each one checks the invoice is in the right state first. Nobody can skip "sent" and jump straight to "paid".
Notice the mix: $number is readonly because it's set once and never changes again, while $status is private(set) because it's meant to change, just not from outside.
readonly, private(set) or a property hook?
Three tools, three jobs:
- readonly: the value is written once and is then fixed for the life of the object, even for the class itself. Use it for identities and values like invoice numbers. For a changed copy, use PHP 8.5's
clone()with new values. - private(set) (or protected(set)): the class keeps changing the value over time and everyone else reads it. Use it for status, counters and collections managed by methods.
- Property hooks: reading needs working out, or writing needs checking or tidying. Use a
getorsethook (also PHP 8.4), or a plain method if the work is heavy.
If you're about to write a getter that only does return $this->something;, the second option is usually the tidier one.
Static properties (PHP 8.5)
From PHP 8.5, static properties can have their own write visibility too:
```php <?php declare(strict_types=1);
final class Mailer { public private(set) static int $sent = 0;
public function send(string $to): void { // ...send the email... self::$sent++; } }
(new Mailer())->send('a@example.com'); (new Mailer())->send('b@example.com');
echo Mailer::$sent, PHP_EOL; // 2
try { Mailer::$sent = 0; } catch (Error $e) { echo $e->getMessage(), PHP_EOL; } ```
Any code can read the count; only Mailer can bump it. The reset attempt prints "Cannot modify private(set) property Mailer::$sent from global scope".
The rules that catch people out
Summarised from the PHP manual's visibility page:
- Only typed properties can have a separate write visibility.
- The write visibility must be the same as the read visibility or tighter.
protected public(set)is a syntax error. - A
private(set)property is automatically final, so a subclass can't redeclare it. - Taking a reference counts as a write, and so does writing to an element of an array property.
$order->items[] = $line;from outside fails, so give the class anaddItem()method. - No spaces inside the brackets:
private( set )is a parse error.
Why it's worth adopting
Fewer getters means less code to read and review, and the rule about who can change what sits right on the property, where the next developer will see it. It's a small change that makes "who changed this?" a much shorter conversation.
The PHP 8.4 release announcement covers asymmetric visibility alongside property hooks, and the PHP 8.5 release page lists the newer features. For property hooks in depth, see PHP 8.4 property hooks: drop the getter and setter boilerplate. For readonly copies, see clone with in PHP 8.5.
Want the bigger habits behind this? PHP Done The Correct Way is on Amazon UK in paperback and on Kindle, with free companion code on the PHP AIR code page. Students working on a PHP assignment can try Pass Your PHP Project on Kindle or in paperback. More practical notes are on the PHP AIR blog, or start at phpair.co.uk.
For a lighter take on the same idea, read the blog post: PHP asymmetric visibility: what public private(set) does