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.
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.