Enforcing Return Type With Spl Classes¶
Until PHP 8.1, the types of the methods that were related to SPL were not validated against their interfaced, as it is the case for other native or custom interfaces.
In PHP 8.1, it is now enforced.
PHP code¶
<?php
class X extends LimitIterator {
function __construct() { echo __METHOD__; }
public function current() {}
}
new X();
?>
Before¶
x::__construct
After¶
PHP Deprecated: Return type of x::current() should either be compatible with IteratorIterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /codes/enforceSplReturnType.php on line 5
Deprecated: Return type of x::current() should either be compatible with IteratorIterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /codes/enforceSplReturnType.php on line 5
x::__construct
PHP version change¶
This behavior changed in 8.1