One of the most sought-after features for PHP is Generics: The ability to have a type that takes another type as a parameter.  It's a feature found in

Compile time generics: yay or nay?

submited by
Style Pass
2025-08-07 12:00:05

One of the most sought-after features for PHP is Generics: The ability to have a type that takes another type as a parameter. It's a feature found in most compiled languages by now, but implementing generics in an interpreted language like PHP, where all the type checking would have to be done at runtime, has always proven Really Really Hard(tm), Really Really Slow(tm), or both.

But, experimentation by the PHP Foundation's dev team suggests we may be able to get 80% of the benefit for 20% of the work. Is that enough?

We believe it's possible to implement generics on only interfaces and abstract classes, which would offer a large chunk of the benefit of generics but avoid most of the pitfalls.

All of this is done at compile time, which makes it far easier and faster, and many/most errors would be caught at compile time.

Runtime generics, where you could say $fooExporter = new Exporter<Foo>(), would still not be possible, but they do not get any harder as a result of going just part way.

Leave a Comment
Related Posts