|  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
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.
Block user comment
Status: Assign to:
Bug Type:
From: enumag at gmail dot com
New email:
PHP Version: OS:


 [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 06:03:14 2021 UTC