Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

php[architect]:
The Case for Generics in PHP
Dec 04, 2018 @ 14:30:55

The php[architect] site has posted an article pulled from their November 2018 issue providing a case for generics in PHP.

To elucidate their merit, we will look at how they might fit within the evolution of PHP’s type system to put us in a position to write more robust software:

“How can I signal that my method will return a Collection of User objects? And why would I want to do that?”

The article starts with a look at generics in other languages and how they're handled (such as Java and C#). From there they shift over to PHP, giving a code sample of a class that is typed to only accept one kind of value for its constructor. They provide an update for this that would work in PHP 7.4 (with typed properties) and how with the help of generics it would be simpler to define a more dynamic type requirement, allowing for more generalized class descriptions.

tagged: phparchitect article november2018 issue generics tutorial php74

Link: https://www.phparch.com/2018/11/the-case-for-generics-in-php/