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?
The short, short version
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.
In particular, interfaces and abstract classes could declare that they need one or more types specified:
interface Exporter
And then classes that implement/extend them would be required to fill in those types:
class WidgetExporter implements Exporter
And then anywhere the type Thing appeared in Exporter , it would turn into Widget in WidgetExporter .
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.
... continue reading