Every time I find myself typing new MyClass()
and than returning back and adding those parentheses. It would be good to reduce this friction and make PHP coding flow smoother.
Cleaner code
Often, I add static methods to some classes in order to improve readability when I instantiate a new class; the ability of chaining methods directly to the constructor, without the unnecessary parenthesis boilerplate, would remove the need for that.
It could be nice addition to creating DTOs objects in PHP with "virtual" properties:
readonly final class SomeDto { public function __construct( public string $name, public string $surname, public string $fullName { get => ucfirst($this->name) . ' ' . ucfirst($this->surname); } ) { } }
or even shorter version:
readonly final class SomeDto { public function __construct( public string $name, public string $surname, public string $fullName => ucfirst($this->name) . ' ' . ucfirst($this->surname), ) { } }
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.
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');
Similar concept exists in Kotlin, Swift (extend protocols), Java, C#, and other languages, and it has proven to be quite useful in those ecosystems.
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