1.2.565. Incompatible Signature Methods

Methods should have the same signature when being overwritten.

The same signatures means the children class must have :

  • the same name

  • the same visibility or less restrictive

  • the same typehint or removed

  • the same default value or removed

  • a reference like its parent

This problem emits a fatal error, for abstract methods, or a warning error, for normal methods. Yet, it is difficult to lint, because classes are often stored in different files. As such, PHP do lint each file independently, as unknown parent classes are not checked if not present. Yet, when executing the code, PHP lint the actual code and may encounter a fatal error.

<?php

class a {
    public function foo($a = 1) {}
}

class ab extends a {
    // foo is overloaded and now includes a default value for $a
    public function foo($a) {}
}

?>

See also Object Inheritance.