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: 2020-09-26 22:53 UTC
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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: spychala dot adam at gmail dot com
New email:
PHP Version: OS:

 

 [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.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Nov 24 16:01:24 2020 UTC