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

Error Messages