Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Parameter Contravariance

PHP added the support of parameter type contravariance. Since PHP 7.4, 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)

Warning: Declaration of y::foo(i $a) should be compatible with x::foo(j $a)
object(y)#1 (0) {
}

After

object(y)#1 (0) {
}

PHP version change

This behavior changed in 7.4.

Error Messages

© 2023-2026, Damien Seguy - Exakat