php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64963 Allow fetching trait a method is copied from in ReflectionMethod
Submitted: 2013-06-03 13:57 UTC Modified: 2021-07-23 12:34 UTC
Votes:18
Avg. Score:4.3 ± 0.8
Reproduced:17 of 17 (100.0%)
Same Version:5 (29.4%)
Same OS:3 (17.6%)
From: alasdair at softhome dot net Assigned:
Status: Open Package: Reflection related
PHP Version: 5.4.15 OS: Debian Wheezy
Private report: No CVE-ID: None
 [2013-06-03 13:57 UTC] alasdair at softhome dot net
Description:
------------
Specific PHP version PHP 5.4.15-1~dotdeb.2

Executing ReflectionClass::getMethods on a class using traits does not 
differentiate between methods within the class or methods within the trait being 
used, whereas calling ReflectionMethod::getDeclaringClass on the class with the 
method identifies it as coming from the trait.

Test script:
---------------
<?php

trait t {
    function f() {
        //do stuff
    }
}

Class c {
    use t;

    function f2() {
        //do stuff
    }
}

$class = new ReflectionClass('c');
print_r($class->getMethods());
?>

Expected result:
----------------
Array
(
    [0] => ReflectionMethod Object
        (
            [name] => f2
            [class] => c
        )

    [1] => ReflectionMethod Object
        (
            [name] => f
            [class] => t
        )

)

or

Array
(
    [0] => ReflectionMethod Object
        (
            [name] => f2
            [class] => c
        )

    [1] => ReflectionMethod Object
        (
            [name] => f
            [trait] => t
        )

)

Actual result:
--------------
Array
(
    [0] => ReflectionMethod Object
        (
            [name] => f2
            [class] => c
        )

    [1] => ReflectionMethod Object
        (
            [name] => f
            [class] => c
        )

)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-03 16:27 UTC] laruence@php.net
-Status: Open +Status: Feedback
 [2013-06-03 16:27 UTC] laruence@php.net
the methods of traits are copied into class.

so, in theory, there is nothing wrong.
 [2013-06-03 17:26 UTC] alasdair at softhome dot net
In that case should there not be some kind of isTrait for the ReflectionMethod 
the same way there is for the ReflectionClass?
 [2013-06-27 08:46 UTC] david at mouf-php dot com
I have a very strong feeling about this.
I understand that the PHP implementation of traits is about copying traits into classes. Yet, the whole purpose of a Reflection API is to find where properties / methods are declared.

I have a simple question: I need to be able to find in which class/trait a method is declared. I need this for my PHP framework (Mouf), that is heavily relying in reflection to build a graphical dependency injection system.

I've written a lengthy blog post explaining what the problems with the current Reflection API are, and hopefully, how to find a (not perfect) work around.

You can find the blog post here: http://mouf-php.com/blog/php_reflection_api_traits

I really hope we can agree this is indeed something that needs to be fixed, as the workaround I've put in place are far from perfect.

For technical details, my PHP version: PHP 5.4.9-4ubuntu2.1.
 [2013-06-27 09:58 UTC] alasdair at softhome dot net
Nice blog David.  Good to see other people battling with this frustration as well.  Will be looking more indepth at your work around to see if it helps what I was planning on doing.
 [2013-10-15 11:54 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2013-10-15 13:05 UTC] alasdair at softhome dot net
Myself and david at mouf-php dot com still feel there is an issue here as I'm sure many other people do as well.

Yes, David has provided a hacky workaround however this is not the solution.

The reason I had not added any more feedback was because I had just abandoned my use of traits due to this behavior, not because the issue had been resolved/
 [2013-10-15 17:14 UTC] nikic@php.net
-Status: No Feedback +Status: Open
 [2013-10-15 17:14 UTC] nikic@php.net
The No-Feedback was an automated status change. I reverted the bug back to Open ;)
 [2014-10-01 22:12 UTC] ocramius at gmail dot com
Note that this also affects various codegen components: the problem is that a trait does not actually exist unless used inside the context of a class.

Methods and members of the trait that were copied into the class context at eval-time should be reflected as members of the class, not of the trait: same goes for the names of the methods, which are the final ones (after eventual aliasing).

Information about the trait itself is pretty much useless except for libraries that extract method body/file information: these could probably need a different API.

Related:
 - https://github.com/zendframework/zf2/issues/6620
 - https://github.com/Ocramius/ProxyManager/issues/147
 [2017-01-24 18:43 UTC] love at sickpeople dot se
I get that changing getDeclaringClass() will generate some discussion.

What about adding getDeclaringTrait()?
 [2017-01-24 18:44 UTC] love at sickpeople dot se
FWIW, since the trait methods are copied, it is essentially declared by both parties.
 [2018-07-30 16:55 UTC] josef dot sabl at gmail dot com
This bug affects Nette Frameworks DI as well (as described here https://github.com/nette/di/issues/169) This surely needs to be fixed. The argument that Trais are copied into classes so this is okay is very weak imho.
 [2020-03-02 12:26 UTC] nikic@php.net
-Type: Bug +Type: Feature/Change Request
 [2020-03-02 12:26 UTC] nikic@php.net
Reclassifying as feature request for a new method to provide the trait prototype method. The current behavior of getDeclaringClass() is correct, but there is space for an additional API here.
 [2021-07-23 12:34 UTC] nikic@php.net
-Summary: ReflectionClass::getMethods does not identify traits +Summary: Allow fetching trait a method is copied from in ReflectionMethod
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 13:01:29 2024 UTC