Asymmetric visibility, introduced in PHP 8.4, can make a property publicly readable but privately or protectedly writable.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP Asymmetric Property Visibility in PHP 8.4 Works
Asymmetric visibility, introduced in PHP 8.4, can make a property publicly readable but privately or protectedly writable.
Compatibility note: Requires PHP 8.4 or later.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
class Task { public private(set) string $status = "new"; public function complete(): void { $this->status = "done"; } }
$task = new Task();
$task->complete();
echo $task->status;
Output:
done
Asymmetric visibility, introduced in PHP 8.4, can make a property publicly readable but privately or protectedly writable.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
class Account { public protected(set) int $balance = 0; public function deposit(int $amount): void { $this->balance += $amount; } }
$account = new Account();
$account->deposit(50);
echo $account->balance;
Output:
50
Write visibility must not exceed read visibility; access from outside the allowed setter scope fails.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
final class Document { public private(set) string $title; public function __construct(string $title) { $this->title = $title; } }
echo (new Document("Guide"))->title;
Output:
Guide
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Write visibility must not exceed read visibility; access from outside the allowed setter scope fails.
- Asymmetric visibility, introduced in PHP 8.4, can make a property publicly readable but privately or protectedly writable.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP Asymmetric Property Visibility in PHP 8.4 | Asymmetric visibility, introduced in PHP 8.4, can make a property publicly readable but privately or protectedly writable. |
| Alternative approach | A public getter with a private backing property achieves similar encapsulation with more boilerplate. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP Asymmetric Property Visibility in PHP 8.4 | Write visibility must not exceed read visibility; access from outside the allowed setter scope fails. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | A public getter with a private backing property achieves similar encapsulation with more boilerplate. |
Key Takeaways
- Asymmetric visibility, introduced in PHP 8.4, can make a property publicly readable but privately or protectedly writable.
- Write visibility must not exceed read visibility; access from outside the allowed setter scope fails.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP Asymmetric Property Visibility in PHP 8.4 is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.