Bug #80153 [Nab]: Self return type and ReflectionClass->getReturnType
| From: | spychala dot adam at gmail dot com | Date: | Sat, 26 Sep 2020 22:21:27 +0000 |
| Subject: | Bug #80153 [Nab]: Self return type and ReflectionClass->getReturnType | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-229228@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
User updated by: spychala dot adam at gmail dot com
Reported by: spychala dot adam at gmail dot com
Summary: Self return type and ReflectionClass->getReturnType
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:
I'm sorry. Repeated mistake. I thought of 'B' and typed 'A'.
I agree with that
> If getReturnType() returned "B" then the fact that the method actually is supposed to
> return "self" is lost.
It should be another method for returning specific return type, because we also miss such
information with self/static return types.
Previous Comments:
------------------------------------------------------------------------
[2020-09-26 21:29:35] requinix@php.net
> Self is not type in PHP, is keyword.
self is most definitely a type. You can use it in every situation I can think of that wants a type:
parameter types, return types, instanceof a type, creating a new instance of a type...
It is *also* a keyword.
> Shouldn't it return then 'A', not 'self'?
Like I said before, no. Please read what I said.
------------------------------------------------------------------------
[2020-09-26 20:38:47] spychala dot adam at gmail dot com
Shouldn't it return then 'A', not 'self'?
Self is not type in PHP, is keyword.
------------------------------------------------------------------------
[2020-09-26 19:33:35] requinix@php.net
"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';
}
------------------------------------------------------------------------
[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