Returntype Covariance

PHP 7.4 added the support of return type covariance. The return type of a child class may be more strict than the one of the parent.

In PHP 7.3, the child method must have the same return type than the parent.

PHP code

<?php

interface i {}

interface j extends i {}

class x {
     function foo() : i {

     }
}

class y extends x {
     function foo() : j {

     }
}

var_dump(new y);

?>

Before

PHP Fatal error:  Declaration of y::foo(): j must be compatible with x::foo(): i in /codes/covarianceReturntype.php on line 17

Fatal error: Declaration of y::foo(): j must be compatible with x::foo(): i in /codes/covarianceReturntype.php on line 17

After

object(y)#1 (0) {
}

PHP version change

This behavior changed in 7.4

Error Messages