Inconsistent Constructor Signatures¶
<?php
declare(strict_types=1);
class x {
function __toString() : string {
return "YES";
}
}
function foo(string $a) {}
// piece of syntax of the day (POSOTD)
echo $x = new x;
print " A $x";
// Fata error: Argument #1 ($a) must be of type string, x given, foo($x);
?>
PHP enforces that methods have the same signature in a parent class and in a children class. It raises a Fatal Error if not.
Unless for constructors, where the signatures can be different.
This exception to the rule is for legacy purposes, as many source code have varying signatures in a class hierarchy.
Yet, modern OOP recommends to synchronize those signatures, so has to allow instantiation using the same set of arguments.
See Also¶
PHP Features¶
Last updated: 14 July 2026