Bug #80153 [Opn->Nab]: Self return type and ReflectionClass->getReturnType
| From: | requinix@php.net | Date: | Sat, 26 Sep 2020 19:33:35 +0000 |
| Subject: | Bug #80153 [Opn->Nab]: Self return type and ReflectionClass->getReturnType | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-229225@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80153&edit=1
ID: 80153
Updated by: requinix@php.net
Reported by: spychala dot adam at gmail dot com
Summary: Self return type and ReflectionClass->getReturnType
-Status: Open
+Status: Not a bug
Type: Bug
Package: Class/Object related
Operating System: Ubuntu
PHP Version: 7.4.10
Block user comment: N
Private report: N
New Comment:
"self" means self. It does not mean "A" or "B". Which means your
expected result is incorrect: B's getSomething needs to return an instance of B, not of A.
https://3v4l.org/YLDaH
If getReturnType() returned "B" then the fact that the method actually is supposed to
return "self" is lost. If you need to know that B::getSomething() must return an instance
of B then I suggest:
if ($return_type == 'self') {
$return_type = 'B';
}
Previous Comments:
------------------------------------------------------------------------
[2020-09-26 11:46:18] spychala dot adam at gmail dot com
Description:
------------
There is question on SO regarding return type of class methods which has 'self' return
type
https://stackoverflow.com/questions/64073093/how-to-get-return-type-of-a-class-method-in-php
I think it should return corresponding return type, not self (or in the future static type).
Test script:
---------------
interface A {
public function getSomething(): self;
}
class B implements A {
public function getSomething(): self
{
return $this;
}
}
print((new ReflectionClass(new B()))->getMethod('getSomething')->getReturnType());
Expected result:
----------------
A
Actual result:
--------------
self
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80153&edit=1