This RFC adds generic type syntax to PHP. Classes, interfaces, traits, functions, methods, closures, and arrow functions can declare type parameters; those parameters carry bounds, defaults, and variance markers; type arguments may be supplied at use sites and at call sites via turbofish.
final readonly class Pair<+L, +R> { public function __construct( public L $left, public R $right, ) {} public function swap(): Pair<R, L> { return new Pair($this->right, $this->left); } } final readonly class Box<+T> { public function __construct( public T $value, ) {} public function map<U>(callable $fn): Box<U> { return new Box(($fn)($this->value)); } public function zip<O>(O $value): Box<Pair<T, O>> { return new Box(new Pair($this->value, $value)); } } function identity<T>(T $value): T { return $value; } $greeting = new Box::<string>("hello, world"); $paired = $greeting->zip::<int>(42); $swapped = $paired->value->swap(); $result = identity::<Pair<int, string>>($swapped);
I'm more in the doubting camp, but there is only YES or NO vote available.
I'm really concerned about the cognitive load while reading the code, it adds complexity, PHP's selling point was its simplicity, features like Aviz, and other make it harder and harder to read and understand quickly.
So, be careful what you wish for)
If you like it or not, generics are already used in the wild. Having a standardized syntax is, in my opinion, a no-brainer.
This RFC chooses its scope carefully, providing some safety where it is easy to do so, while delegating the rest to static analyzers and keeping backwards compatibility. With this, the different static analyzers can finally agree on certain behaviors, because they are part of the language.
Having the generic type information available through reflection opens up exciting new possibilities.
Keeping the doors open for future expansion is another very important win.
TL;DR: The RFC is very pragmatic given the design of PHP. Provides very solid foundation upon which to build that was missing and asked for a decade.
This would easily be one of the best features of modern PHP. Nobrainer.
Looking forward to this feature landing in PHP 8.6.
Long awaited.
Yes please! This would remove a lot of docblocks everywhere. Huge PHPStan user here.
Yes! Time to retire the non-spec docblocks
I use them everywhere.
Generics are a necessary feature for all modern languages. It can reduce so much clutter and is desperately needed.
after so many years, the seems like the least obtrusive way to add generics. Really hope to see it get accepted so we don't have to keep waiting. Seems like a no-brainer tbh.
.
It's an opportunity to modernize PHP.
This feature of native generics syntax in PHP is all I ever dream of..
Chain method on newly created objects without parentheses
A new syntax for declaring the “set” operation visibility of an object property