Member since
We should accept any RFC increasing the capacity of PHP to design things elegantly. Which is done by this suggestion.
I voted "Yes".
PHP misses linear function composition... until now.
Getters and setters are more and more considered bad : https://www.infoworld.com/article/2073723/why-getter-and-setter-methods-are-evil.html
This features feels like a C# fix applied on PHP which I don't like. We should focus on designing better classes, not fix a bad pattern.
I don't understand "no" voters arguing about readability when use (...) is the one statement decreasing readability.
Voted "yes" because I think the opposite :
fn ($something) { return $somethingFromOuterScope . $something; }
Is easy to read... $somethingFromOuterScope can not come from anywhere but only from the most close outer scope...
This kind of code looks very appealing when performing very simple (and anemic) CRUD operations on a model, but it has a very short trajectory when the code gets a little more complex.
Triggering events is shown as an example of an advantage of this feature, but it's clearly a bad idea: https://3v4l.org/ZCVpG#v8.2.11
Also, if you need a centralized validation around an attribute, a value object is a way better option: https://3v4l.org/QWm8c#v8.2.11
I think that there are a ton of better requests with a lot more of value to achieve this in userland code.
Also, this idea is very overlooked, and would require a lot of internals work after to cover all the edge cases. How should child classes behave? Are they overridable? How do you know if there is a setter behavior defined already? How should traits behave? Is there a way to call the parent class setter/getter? Would that be overriden or just called by default?
Also, in response to some comments:
It could be used by ORMs like Laravel Eloquent Model that has cast and other hooks to transform data on get/set.
Good ORMs should provide mechanisms for this!
However, I actually often find the need for more fine-grained control over input and output, but adding methods feels so heavy-weight.
This is the perfect case for value objects to kick in. You want behavior (input validation, output transformation maybe) at an atomic member, that could be exactly this but atomically standalone, and therefore reusable in other cases, and very easily testable.
While I'm not against the concept in general, this implementation is not well-done. The $value variables comes from nowhere and makes it confusing - it looks like an undefined variable and a quick glance at it, was the first thing I thought - where is $value coming from? Why not just use $name within that block? Same with $field? Why not just return the value that is going to be set? This is one of the more confusing RFCs I've seen and does not follow the PHP-code style, makes things confusing for both new and experienced coders. In it's current format, I can't approve with good conscience.
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.
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+).
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...