The PHP Asymmetric Visibility RFC introduces a significant enhancement to PHP’s property visibility control.
Traditionally, PHP properties have symmetric visibility, meaning the same visibility rules apply to both read and write operations.
This RFC proposes a change to allow asymmetric visibility, where properties can have different visibility modifiers for read and write operations.
This new feature aims to provide developers with more granular control over class properties, enhancing encapsulation and API design flexibility.
Asymmetric visibility allows developers to define separate visibility rules for reading and writing a property. For instance, a property can be public for read operations but private or protected for write operations.
The proposed syntax borrows from Swift’s access control and looks as follows:
class Foo {
public private(set) string $bar = 'baz';
}
$foo = new Foo();
echo $foo->bar; // Outputs: baz
$foo->bar = 'beep'; // Visibility error
In this example, the property $bar
can be read publicly but can only be modified privately within the class.
Constructor Property Promotion: Asymmetric visibility can be used in constructor property promotion, which simplifies the initialization of objects.
class Foo {
public function __construct(public private(set) string $bar) {}
}
Abbreviated Form: To ease the common usage pattern, if a set visibility is specified, the public get visibility may be omitted.
private(set) string $foo;
protected(set) string $foo;
Inheritance Rules: Asymmetric visibility respects PHP’s current inheritance rules, ensuring compatibility and avoiding breaking changes. However, properties declared with private(set)
are implicitly final to prevent them from being redeclared with a wider set visibility in child classes.
The asymmetry in visibility will not interact with property hooks introduced in the Property Hooks RFC.
These features operate independently, and a property’s asymmetric visibility won’t affect the behavior injected by hooks. The only exception is for virtual properties that lack a set operation, which will generate a compile error if visibility for the set operation is specified.
Interfaces and abstract classes can declare property requirements with get and set operations separately. Asymmetric visibility can effectively satisfy these requirements, provided the operations align with the specified visibility in the interface.
Encapsulation Enhancement: Developers can expose internal states safely. For instance, a readonly
implementation can now be replaced with a more flexible protected(set)
or private(set)
.
Struct-like Classes: It simplifies the creation of classes that behave like data structs but require controlled mutability.
class Book {
public function __construct(private(set) string $title, private(set) Author $author, private(set) int $pubYear) {}
}
Controlled References: Obtaining references to properties obeys the set visibility, thus preventing unauthorized modifications while allowing read access in the desired scope.
The RFC lays the groundwork for potential future enhancements, such as additional visibility controls for package-level visibility or operations like reference obtaining (protected(&get)
).
These expansions would integrate seamlessly with the proposed asymmetric visibility syntax.
The PHP Asymmetric Visibility RFC represents a thoughtful extension to the existing visibility controls, addressing several limitations of symmetric property visibility.
By adopting this RFC, PHP developers gain finer control over property access, enhancing both encapsulation and flexibility in API design.
This proposal is a testament to PHP’s evolving capability to offer more robust, developer-friendly features without compromising backward compatibility.
Do you own a company or need help with your Laravel project? I can help you with that. Check the plans and let me know if you have any questions.
Get 1 month free with yearly plan during checkout.
$ 3 ,995
/m
What's included:
$ 5 ,995
/m
What's included:
Ok, just book a call now.
Good question! For starters, the annual cost of a full-time senior-level developer now exceeds $100,000, plus benefits (and good luck finding one available).
Aside from that, you may not always have enough work to keep them busy at all times, so you're stuck paying for time you aren't able to utilize.
With the monthly plan, you can pause and resume your subscription as often as you need to ensure you're only paying your developer when you have work available for them.
No, once subscribed you're able to request as many things as you'd like, and they will be delivered one by one.
Delivery will always be fast without compromising quality. Very complex requests will be divided into smaller products for continuous delivery.
Always, before starting to work, we can make a planning so that you know, before starting, when each change will be delivered.
You'll work directly with me, the founder and only person behind DeveloperJoy.
We understand you may not have enough work to fill up entire month. Perhaps you only have one or two requests at the moment. That's where pausing your subscription comes in handy.
Billing cycles are based on 31 day period. Let's say you sign up and use the service for 21 days, and then decide to pause your subscription. This means that the billing cycle will be paused and you'll have 10 days of service remaining to be used anytime in the future
I had experience making products with PHP, Go, and JavaScript/TypeScript.
Also I feel really comfortable working with WordPress, Laravel, Symfony, Vue.js, React, Svelte, and more.
Mail me and we will see how best to collaborate.
You can make this request by email, sharing a task board with me or, depending on your plan, by call or Slack.
That's fine. You can pause your subscription when finished and return when you have additional requests needs. There's no need to let the remainder of your subscription go to waste.
Due to the high quality nature of the work, there will be no refunds issued.