2021/11/25/PHP documentation gripe
The official PHP documentation says:
Covariance allows a child's method to return a more specific type than the return type of its parent's method. Whereas, contravariance allows a parameter type to be less specific in a child method, than that of its parent.
...and then goes on to note that both are supported in #PHP7.4 (which is what I am using).
However:
abstract class cClass1 {
abstract function GetIt() : mixed;
}
abstract class cClass2 extends cClass1 {
abstract function GetIt() : object;
}
PHP Fatal error: Declaration of cClass2::GetIt(): object must be compatible with cClass1::GetIt(): mixed in /home/htnet/site/git/ferreteria/base/tests/php2.php on line 6
...and also...
abstract class cClass1 {
abstract function SetIt($v);
}
abstract class cClass2 extends cClass1 {
abstract function SetIt(object $v);
}
PHP Fatal error: Declaration of cClass2::SetIt(object $v) must be compatible with cClass1::SetIt($v) in /home/htnet/site/git/ferreteria/base/tests/php2.php on line 6
So, kinda no.
The examples given in the documentation seem to be only about specificity of object types -- so I should be able to do this:
class cClassA {}
class cClassB extends cClassA {}
abstract class cClass1 {
abstract function SetIt(cClassA $v);
}
abstract class cClass2 extends cClass1 {
abstract function SetIt(cClassB $v);
}
...but, umm:
PHP Fatal error: Declaration of cClass2::SetIt(cClassB $v) must be compatible with cClass1::SetIt(cClassA $v) in /home/htnet/site/git/ferreteria/base/tests/php2.php on line 8
Looking at the actual example in the docs:
[in progress]