Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Typed arrays in PHP

Tim Bezhashvyly recently wrote an article in which he explains an interesting approach to make sure all items in array are of a certain type. It leverages variadic functions which were introduced in PHP 5.6

Consider this piece of code (borrowed from Tim’s post):

function foo (Product ...$products )
/* ... */

In PHP 7 you can even using the scalar types, such as string and int, to make sure all elements are of that type.

Read Tim’s full article here: https://thephp.cc/news/2016/02/typed-arrays-in-php

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band.
  • Cool! Although, still a ghetto version of typed arrays. :/