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've been writing about this for years. If we ever want generics in PHP, this is the way.
I don't often use generic definitions in docblocks today, but when I do, it's a mess. I think this RFC is the right move at the right time, and cost (both runtime and syntax) appears zero to minimal at best. It'd be really great to see generics land in PHP. It's been a long time coming.
Generics are one of the most requested feature since PHP met the OOP paradigm and for me, this will step to set the boundaries to its OOP complete adherence.
We will be able to describe complex classes or dynamically typed ones, increase readability and move type hinting away from comments.
This is the best of all worlds. Other generic approaches have been tried and never got too far.
Bound-Erased Generics gives us a chance to get a first-party syntax that will make developers happy, allow tooling like IDEs and static analysers to pick up on it, while still leaving the implementation open for another kind of runtime/compile time generic implementation later down the line if the other possible issues with those are solved.
It's about time :)
doc blocks are ugly, needs more lines of code. this rfc is the way to go.
This might be the most important PHP RFC of this decade.
Maybe the only missing feature.
This RFC proposes a way to have multi-line short closures — closures that don't need explicit use statements, but can still have multiple lines and a return statement.
Interface Default Methods improves backwards compatibility when changing interfaces, but also add a way to have multi-inheritance in PHP.
A new syntax for declaring the “set” operation visibility of an object property