Hello,
I would like to gather interest in adding Uniform Function Call Syntax (UFCS)
to the PHP language. In short, it allows to call any static function `f($a)` as
`$a->f(...)`. The `...` is required because not all functions have the desired
parameter in the first position. Outlined below are some benefits of this
syntax.
It allows for chaining of function calls that otherwise would look ugly. A
common pattern is filtering and mapping arrays:
```php
$arr = [1, 2, -3, 4, 0, 5];
$filtered = \array_filter($arr, fn ($x) => $x > 0);
$result = \array_map(fn ($x) => $x * 2, $filtered);
\var_dump($result);
```
This can be written more eloquently:
```php
[1, 2, -3, 4, 0, 5]
->\array_filter(..., fn ($x) => $x > 0)
->\array_map(fn ($x) => $x * 2, ...)
->\var_dump(...);
```
Another use case is for extension functions:
```php
class Cat
{
public function __construct(public string $name) {}
public function meow(): self
{
echo "{$this->name} meowed\n";
return $this;
}
public function lickPaw(): self
{
echo "{$this->name} licked paw\n";
return $this;
}
}
class CatExtension
{
public static function scratch(Cat $cat): Cat
{
echo "{$cat->name} scratched\n";
return $cat;
}
}
$cat = (new Cat('Tabby'))
->meow()
->(CatExtension::scratch)(...)
->lickPaw();
```
For my proposal, only static functions need to be supported, but I don't see
why non-static functions can't be supported. One thing I'm unsure of is what
happens if an imported static function has the same name as a member function.
The Wikipedia page has a bit more details, as well as examples of languages
which implement this syntax:
https://en.wikipedia.org/wiki/Uniform_Function_Call_Syntax.
Please let me know your questions, opinions, and feedback.
Regards,
Yakov