1.2.567. Incompatible Signature Methods With Covariance¶
Methods should have the compatible signature when being overwritten.
The same signatures means the children class must have :
the same name
the same visibility or less restrictive
the same contravariant typehint or removed
the same covariant return 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, PHP RFC: Covariant Returns and Contravariant Parameters and Incompatible Signature Methods.
1.2.567.2. Connex PHP features¶
1.2.567.2.1. Suggestions¶
Make signatures compatible again
1.2.567.2.2. Specs¶
Short name |
Classes/IncompatibleSignature74 |
Rulesets |
|
Exakat since |
1.3.3 |
PHP Version |
With PHP 7.4 and more recent |
Severity |
Critical |
Time To Fix |
Quick (30 mins) |
Precision |
Medium |
Examples |
|
Available in |