Thanks to readonly properties, I started relying more and more on properties alone. However, I actually often find the need for more fine-grained control over input and output, but adding methods feels so heavy-weight. Property hooks feels like the perfect solution for some of my use cases.
In combination with Asymmetric visibility this will allow to replace all getters and setters with trivial properties and occasional hooks.
This is good to have.
Properties are useful for exchanging (reading and writing) single values. Properties are good for data binding, etc.
With this RFC we can implement:
Update: About the $field I am not sure. Having a separate backing field can have some advantages.
In general I would love to see this added to PHP
It would make it easier to debug a flow as well even without tools like XDebug because during development you would be able to insert a |> dd(...)
for instance
For example:
$result = "Hello World"
|> 'htmlentities'
|> 'str_split'
|> fn($x) => array_map('strtoupper', $x)
|> fn($x) => array_filter($x, fn($v) => $v != 'O');
Is 'nicer' since it has better readability and clearer syntax than:
$result = array_filter(
str_split(
strtoupper(
htmlentities("Hello World")
)
),
fn($v) => $v != '0'
);
With First-class callable syntax available since 8.1, it would now be possible to write it as below, which is much better then string names of functions:
$result = "Hello World" |> htmlentities(...) |> str_split(...) |> fn($x) => array_map(strtoupper(...), $x) |> fn($x) => array_filter($x, fn($v) => $v != 'O');
For me, the most important argument is that the pipeline pattern is a tried and tested pattern, that this RFC builds upon. A couple of examples:
This RFC adds syntax to make using these kinds of pattern much more convenient.
On top of that, there's the argument that multiple modern languages support a pipe operator:
Finally, I've had numerous occasions where a pipe operator would simplify my own code — I have more than a handful real life cases where this would be useful.
The use statements are usually what is keeping me from using the short closures, so having this resolved would make life a lot easier in those cases and the code cleaner
PHP is evolving. There are new concepts added to many programming languages to ease writing and reading (more important!). PHP should focus more on developer experience (but not for legacy projects that get never upgraded to PHP 8+).
It will solve having to create traits to add a default implementation when creating interfaces and keeping it nicely together improving the DX
Multi-inheritance seems to be the hot topic that prevented this RFC from being approved even though it was not the RFC target. Multi-inheritance is an afterthought that may or may not be abused with this change. What we want would actually be just the convenience of doing what Traits already allow while reducing potential BC break impact coming from interface changes. There are interfaces originated from the interface segregation mindset that often has only 1 implementation and could very well take advantage of default implementation for a simpler system design.
We spend a lot more time reading code than writing it. The elegance of short closure combined with the convenience of variable scope usage has already shown to be a game changer on Typescript and there doesn’t seem to be any technical issue with having it on PHP.
At least once a week, I throw away an array_map because it ended up looking too bloated and go with a classic foreach instead. Short Closures 2.0 without the use(...) block would've solved this problem, just 2 votes...
Creating traits for default implementation is just a pain. I want syntactic sugar
I wrote down some thoughts on this RFC on my blog. I think it's worth rethinking our current definition of what "an interface" is. Especially since many languages are interface default methods as their way of multi-inheritance.