Exploring Asymmetric Property Visibility in PHP 8.4
The release of PHP 8.4 introduces a powerful new feature: Asymmetric Property Visibility, enabling developers to define separate visibility rules for reading and writing properties. This advancement provides a new level of granularity in controlling access to class properties, enhancing security and encapsulation.
What Is Asymmetric Property Visibility?
Traditionally in PHP, the visibility of class properties (public, protected, or private) applied uniformly to both reading (get) and writing (set) operations. With PHP 8.4, you can now define different visibility scopes for these operations. For instance, a property can be readable publicly but writable only within the class or by its descendants.
Key Points:
Separate visibility for reading and writing: The
get
visibility can be more permissive than theset
visibility but not vice versa.Typed properties only: Asymmetric visibility requires properties to be explicitly typed.
Granular control: Developers can control property access at a more refined level, balancing usability and protection.
Example: Asymmetric Property Visibility
Here's an example to illustrate how this feature works:
class Book
{
public function __construct(
public private(set) string $title, // Publicly readable, privately writable
public protected(set) string $author, // Publicly readable, protected writable
protected private(set) int $pubYear // Protected readable, privately writable
) {}
}
class SpecialBook extends Book
{
public function update(string $author, int $year): void
{
$this->author = $author; // OK (protected writable)
$this->pubYear = $year; // Fatal Error (private writable)
}
}
$b = new Book('How to PHP', 'Peter H. Peterson', 2024);
echo $b->title; // Outputs: How to PHP
echo $b->author; // Outputs: Peter H. Peterson
echo $b->pubYear; // Fatal Error (protected visibility)
// Attempting to modify properties
$b->title = 'New Title'; // Fatal Error (private writable)
$b->author = 'New Author'; // Fatal Error (protected writable)
$b->pubYear = 2023; // Fatal Error (private writable)
Rules and Caveats
Typed Properties Only: Asymmetric visibility is allowed only on properties with an explicit type declaration.
Set Visibility Rules: The
set
visibility must be the same or more restrictive than theget
visibility:
✅public private(set)
✅protected private(set)
❌protected public(set)
(syntax error)Finality of
private(set)
: Properties withprivate(set)
are automaticallyfinal
and cannot be overridden in child classes.References and Arrays: Accessing a reference or modifying an array property internally uses both
get
andset
visibility. As a result, the stricterset
visibility applies.Syntax Rules: Spaces within the
set
visibility declaration are not allowed. Useprivate(set)
instead ofprivate( set )
to avoid parse errors.
Inheritance Considerations
Child classes can redefine properties of a parent class if the property is not declared final
. The new visibility must align with the parent's visibility rules:
class Book
{
protected string $title;
public protected(set) string $author;
protected private(set) int $pubYear;
}
class SpecialBook extends Book
{
public protected(set) string $title; // OK: Reading wider, writing same.
public string $author; // OK: Reading same, writing wider.
public protected(set) int $pubYear; // Fatal Error: private(set) properties are final.
}
Benefits of Asymmetric Property Visibility
Enhanced Encapsulation: By separating read and write visibility, developers can create more secure and well-structured classes.
Improved Flexibility: Publicly expose properties for reading without compromising their integrity by restricting writes.
Better Maintainability: Clearer property access policies reduce bugs and make the code easier to understand.
Conclusion
Asymmetric Property Visibility in PHP 8.4 is a powerful addition, offering developers more nuanced control over property access. By enforcing clear rules and maintaining backward compatibility, PHP 8.4 ensures this feature enhances, rather than complicates, property management.
Follow this article for more updates as PHP 8.4 rolls out its full feature set: What’s New in PHP 8.4: Key Enhancements and Updates.
🔥 Supercharge Your Development with Spec Coder AI
👉 Download Spec Coder AI today and start coding like a pro!
Unlock 50% off! Don't miss out, use code "YT-FAMILY" at checkout (limited-time offer).
Please login or create new account to add your comment.