Content · Web Development · PHP

PHP 8.4 Asymmetric Visibility: Practical Examples

PHP 8.4 allows properties to have more restrictive write visibility than read visibility.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Admin

PHP 8.4 allows properties to have more restrictive write visibility than read visibility.

What It Does and When to Use It


Give callers read access while restricting writes to the declaring class. A minimum platform requirement controls what syntax can be parsed; target-version tests find runtime and extension incompatibilities.

Requirements, Inputs and Expected Behavior


Confirm the runtime, extension, environment variables, and permissions before running the examples in production. Use public private(set) for readable state that only the class can update; syntax needs PHP 8.4+.

Example 1: Basic Operation


Give callers read access while restricting writes to the declaring class.

<?php
final class Profile {
     public private(set) string $status = 'pending';
     public function activate(): void { $this->status = 'active'; }
 }
 $profile = new Profile();
 $profile->activate();
 echo $profile->status;

Example 2: Behavior and Validation


Use a controlled mutator to preserve an invariant.

<?php
final class Counter {
     public private(set) int $value = 0;
     public function increment(): void { ++$this->value; }
 }
 $counter = new Counter(); $counter->increment();
 echo $counter->value;

Practical Example: Application Use


Contrast protected(set) with private(set) for subclasses.

<?php
class Document {
     public protected(set) string $title = 'Draft';
 }
 class EditableDocument extends Document {
     public function rename(string $title): void { $this->title = $title; }
 }
 $d = new EditableDocument(); $d->rename('Final'); echo $d->title;

Important Behavior and Edge Cases


The second example highlights: Use a controlled mutator to preserve an invariant. The practical example adds: Contrast protected(set) with private(set) for subclasses.

Comparison with Related Approaches


ApproachWhat changes
Minimum PHP version and target PHP versionA minimum platform requirement controls what syntax can be parsed; target-version tests find runtime and extension incompatibilities.
Choosing safelyDo not introduce syntax from a newer minor version before every production host supports it.

Common Beginner Mistakes


MistakeCorrect approach
Assuming successful execution proves the result is usableCheck the return value, required extension, and observable output of the specific operation.
Ignoring runtime and environment differencesVerify PHP version, SAPI, operating system, service access, and permissions before deployment.
Skipping operational constraintsUse public private(set) for readable state that only the class can update; syntax needs PHP 8.4+.

Key Takeaways


  • PHP 8.4 allows properties to have more restrictive write visibility than read visibility.
  • Contrast protected(set) with private(set) for subclasses.
  • Do not introduce syntax from a newer minor version before every production host supports it.
Share this resourceSend this page to someone who may find it useful.