Bug #79754 [Opn->Nab]: Covariance fails with mixed type
| From: | derick@php.net | Date: | Sun, 28 Jun 2020 13:32:28 +0000 |
| Subject: | Bug #79754 [Opn->Nab]: Covariance fails with mixed type | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-227709@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79754&edit=1
ID: 79754
Updated by: derick@php.net
Reported by: raincomplain at outlook dot com
Summary: Covariance fails with mixed type
-Status: Open
+Status: Not a bug
Type: Bug
Package: Class/Object related
PHP Version: 8.0.0alpha1
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
This is expected behaviour. If a return type is missing, it is the implicit union of
"mixed|void". As "mixed|void" is a wider type than just "mixed",
allowing it would break LSP.
This is also explained in the RFC: https://wiki.php.net/rfc/mixed_type_v2#signature_checking_of_function_when_no_return_type_present
Previous Comments:
------------------------------------------------------------------------
[2020-06-28 13:06:19] raincomplain at outlook dot com
Description:
------------
Since mixed type indicates the absence of a type then one would assume that omitting a return type
in a child class's method that returns mixed type in a parent class is fine! but PHP refuses to
compile
Test script:
---------------
class Foo {
function fun(): mixed {
return 33;
}
}
class Bar extends Foo {
function fun() {
return 33;
}
}
Expected result:
----------------
The expected is to compile with no error
Actual result:
--------------
Fatal error: Declaration of Bar::fun() must be compatible with Foo::fun(): mixed
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79754&edit=1