Bug->Req #80153 [Nab->Opn]: ReflectionMethod should have a method to translate the "self" return type
| From: | requinix@php.net | Date: | Sat, 26 Sep 2020 22:53:41 +0000 |
| Subject: | Bug->Req #80153 [Nab->Opn]: ReflectionMethod should have a method to translate the "self" return type | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-229229@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
+Summary: ReflectionMethod should have a method to translate
the "self" return type
-Status: Not a bug
+Status: Open
-Type: Bug
+Type: Feature/Change Request
Package: Class/Object related
Operating System: Ubuntu
PHP Version: 7.4.10
Block user comment: N
Private report: N
New Comment:
Ah. In that case,
> Shouldn't it return then 'B', not 'self'?
I don't think so. Reflection isn't just about how code behaves but about how it was
written, and if the method was written to return "self" then that is what reflection
should say.
But a new method to return the "effective" type sounds like a reasonable request to me.
Previous Comments:
------------------------------------------------------------------------
[2020-09-26 22:21:26] spychala dot adam at gmail dot com
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.
------------------------------------------------------------------------
[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