Parameter Contravariance¶
PHP 7.4 added the support of parameter type contravariance. The parameter type of a child class may be less strict than the one of the parent.
In PHP 7.3, the child method must have the same parameter type than the parent.
PHP code¶
<?php
interface I {}
interface J extends I {}
class X {
function foo(j $a) {}
}
class Y extends X {
function foo(i $a) {}
}
var_dump(new Y);
?>
Before¶
PHP Warning: Declaration of y::foo(i $a) should be compatible with x::foo(j $a) in /codes/contravarianceParameter.php on line 17
Warning: Declaration of y::foo(i $a) should be compatible with x::foo(j $a) in /codes/contravarianceParameter.php on line 17
object(y)#1 (0) {
}
After¶
object(y)#1 (0) {
}
PHP version change¶
This behavior changed in 7.4