1.2.1332. Use Covariance¶
Covariance is compatible return typehint. A child class may return an object of a child class of the return type of its parent’s method.
Since a children class may return a children class of the return type, the evolution is in the same order.
Covariance is a PHP 7.4 feature. Covariance is distinct from argument contravariance.
<?php
class X {
function m(Y $z): X {}
}
// m is overwriting the parent's method.
// The return type is different.
// The return type is compatible, as Y is also a sub-class of X.
class Y extends X {
function m(X $z): Y {}
}
?>
See also Covariant Returns and Contravariant Parameters and :ref:`No title for `Php/UseContravariance`_ <No anchor for `Php/UseContravariance`_>`.
1.2.1332.1. Connex PHP features¶
1.2.1332.1.1. Specs¶
Short name |
Php/UseCovariance |
Rulesets |
|
Exakat since |
1.9.3 |
PHP Version |
With PHP 7.4 and more recent |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Available in |