php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80153 ReflectionMethod should have a method to translate the "self" return type
Submitted: 2020-09-26 11:46 UTC Modified: 2021-09-10 10:33 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: spychala dot adam at gmail dot com Assigned:
Status: Open Package: Class/Object related
PHP Version: 7.4.10 OS: Ubuntu
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
50 - 43 = ?
Subscribe to this entry?

 
 [2020-09-26 11:46 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-09-26 19:33 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2020-09-26 19:33 UTC] 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 20:38 UTC] 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 21:29 UTC] 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 22:21 UTC] 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 22:53 UTC] requinix@php.net
-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
 [2020-09-26 22:53 UTC] requinix@php.net
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.
 [2021-09-10 10:33 UTC] cmb@php.net
> But a new method to return the "effective" type sounds like a
> reasonable request to me.

I have doubts about that, because it's not hard to resolve that
for `self` in userland code, and for `static` it would not be
possible at all to have such a method (unless it accepted the LSB
type, in which case it would not be hard to resolve that in
userland, too).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 20:01:29 2024 UTC