What’s new in the upcoming PHP 7.4?

Good overview by Brendt on the new features that will be landing in PHP 7.4, which is due somewhere December 2019.

I’m especially looking forward to Typed properties.

New in PHP 7.4 →

šŸ’­ One thing I personally find missing in PHP’s typehinting, is a way to typehint array contents ā€” e.g. ā€œI’d like an array of Foo‘sā€.

The addition of a [] suffix on the hinted type ā€“ like Java has ā€“ could potentially solve this. In the mean time you can resort to hackery like this (src):

// Definition
class ArrayOfFoo extends \ArrayObject {
    public function offsetSet($key, $val) {
        if ($val instanceof Foo) {
            return parent::offsetSet($key, $val);
        }
        throw new \InvalidArgumentException('Value must be a Foo');
    }
}

// Usage
function workWithFoo(ArrayOfFoo $foos) {
    foreach ($foos as $foo) {
        // etc.
    }
}

I anyone has a better way to tackle this, feel free to enlighten me by leaving a comment.

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.