|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #68742 ReflectionProperty::getDeclaringClass doesn't work with traits
Submitted: 2015-01-04 14:12 UTC Modified: 2016-06-18 12:38 UTC
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: enumag at gmail dot com Assigned: cmb (profile)
Status: Duplicate Package: Reflection related
PHP Version: 5.6.4 OS: any
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 !
Your email address:
Solve the problem:
33 + 31 = ?
Subscribe to this entry?

 [2015-01-04 14:12 UTC] enumag at gmail dot com
Use case: I need to read the @var annotation and fix it using the use statements from that file. For that I need to know the file where the property was declared which could be normally found out from ReflectionClass::getFile.

However ReflectionProperty::getDeclaringClass doesn't return the trait where the property was actually declared but only the class that uses the trait. ReflectionMethod::getDeclaringClass suffers from this bug as well.

More information and a workaround can be found in this article.

Test script:

trait Foo
    public $foo;
    public function foo() {}

class Bar
    use Foo;

$r = new ReflectionProperty('Bar', 'foo');
echo $r->getDeclaringClass()->getName();

echo "\n";

$r = new ReflectionMethod('Bar', 'foo');
echo $r->getDeclaringClass()->getName();

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-18 12:38 UTC]
-Status: Open +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2016-06-18 12:38 UTC]
Duplicate of bug #64963.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Nov 29 11:03:18 2021 UTC