note 126464 added to language.types.declarations
| From: | crash at main dot php dot net | Date: | Wed, 29 Sep 2021 13:02:28 +0000 |
| Subject: | note 126464 added to language.types.declarations | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217014@lists.php.net to get a copy of this message | ||
The documentation lacks the information, that it's possible to change the return type of a
method defined in an interface when the interface's methods return type is defined as
mixed.
From the RFC:
"The mixed return type could be narrowed in a subclass as this is covariant and is allowed in
LSP." (https://wiki.php.net/rfc/mixed_type_v2)
This means the following code is valid in PHP 8.0:
<?php
interface ITest
{
public function apfel(): mixed; // valid as of 8.0
}
class Test implements ITest
{
public function apfel(): array // more explicit
{
return [];
}
}
var_dump((new Test())->apfel());
?>
You can see the result here: https://3v4l.org/PXDB6
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 213.252.163.62)
----
Manual Page -- https://php.net/manual/en/language.types.declarations.php
Edit -- https://main.php.net/note/edit/126464
Del: integrated -- https://main.php.net/note/delete/126464/integrated
Del: useless -- https://main.php.net/note/delete/126464/useless
Del: bad code -- https://main.php.net/note/delete/126464/bad+code
Del: spam -- https://main.php.net/note/delete/126464/spam
Del: non-english -- https://main.php.net/note/delete/126464/non-english
Del: in docs -- https://main.php.net/note/delete/126464/in+docs
Del: other reasons-- https://main.php.net/note/delete/126464
Reject -- https://main.php.net/note/reject/126464
Search -- https://main.php.net/manage/user-notes.php