Would prove very useful in more complex array_map or array_filter usages
Nice addition, but I would change syntax:
class User { public string $name { set($value) { if (strlen($value) === 0) { throw new ValueError("Name must be non-empty"); } return $value; } } public function __construct(string $name) { $this->name = $name; } }
In combination with Asymmetric visibility this will allow to replace all getters and setters with trivial properties and occasional hooks.
I think this would be a great improvement.
I prefer this:
$result = "Hello World" |> htmlentities(...) |> str_split(...) |> fn($x) => array_map(strtoupper(...), $x) |> fn($x) => array_filter($x, fn($v) => $v != 'O');
instead of this:
$result = "Hello World" |> 'htmlentities' |> 'str_split' |> fn($x) => array_map('strtoupper', $x) |> fn($x) => array_filter($x, fn($v) => $v != 'O');
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');
I could have used it multiple times for array transformations. But the RFC still built on really ancient PHP behaviour (mapping functions as strings) and should be redone by fosucing only on modern syntax:
|> fn($x) => array_filter($x, fn($v) => $v != 'O')
|> str_split(...)
Makes code more clean because it is shorter and the use
keyword is not needed anymore.
Since we have already autocaptures for one liners there should be also option for multiple liners.
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...