Typed Arrays in PHP


Tim Bezhashvyly:

Typed arrays exist in PHP, at least to some degree. This wonderful feature sneaked in as a side-effect of variadic functions that were added to the language in PHP 5.6.

Turns out one can actually typehint the variadic parameter, as so:

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

Only works if your function has one argument though (or if the β€œtyped array” is the last parameter to your function). Otherwise you’ll get a Fatal Error Only the last parameter can be variadic

Typed Arrays in PHP →

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.