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

Stitcher.io:
PHP Generics and why we need them
May 23, 2017 @ 14:48:56

On the Stitcher.io blog there's a post that's advocating the addition of a feature to the core PHP language that several others have: generics. The basic idea behind generics is to provide functionality that allows the definition of entity types that can be reused as a default (like a "collection" generic that could be used instead of a base collection class).

In today's blog post we'll explore some common problems with arrays in PHP. All the problems and issues listed could be solved with a pending RFC which adds generics to PHP. We won't explore in too much detail what generics are. But at the end of this read, you should have a good idea as to why they are useful, and why we really want them in PHP. So without further ado, let's dive into the subject.

They start off with a common use case in PHP: a collection of blog posts and getting information from them. There's no guarantee that the contents of the set are always post objects leading to specific checks to ensure data quality before use. The post then uses a common example from PHP, the creation of a collection class to handle a set of objects and then showing how (with pseudo-code as generics aren't in the language) how that same functionality might look defined as a generic collection instead.

tagged: generics language feature example rfc collection

Link: https://www.stitcher.io/blog/php-generics-and-why-we-need-them


Trending Topics: