Explain 'Asymmetric Visibility' introduced in PHP 8.4. When would you use private(set)?
Last updated
Asymmetric Visibility gives a property separate read and write scopes, e.g.
public private(set) int $id;(the set scope must be equal or stricter).Use
private(set)for values callers may read but only the class may change, like an$idor$status.Unlike
readonly, the class can still reassign it after construction, and no public getter is needed.
- https://wiki.php.net/rfc/asymmetric-visibility-v2
- https://www.zend.com/blog/php-asymmetric-visibility
- https://stitcher.io/blog/readonly-or-private-set
- https://www.php.net/manual/en/language.oop5.visibility.php
- https://stitcher.io/blog/new-in-php-84
- https://kvnc-inc.medium.com/embracing-php-8-4s-asymmetric-visibility-a-game-changer-for-clean-code-83cbaf705885
- https://dev.to/gabrielanhaia/php-84-asymmetric-visibility-5-patterns-that-replace-constructors-and-setters-3pb9