php.net |  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
Votes:2
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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: enumag at gmail dot com
New email:
PHP Version: OS:

 

 [2015-01-04 14:12 UTC] enumag at gmail dot com
Description:
------------
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.
http://mouf-php.com/blog/php_reflection_api_traits

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

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:
----------------
Foo
Foo

Actual result:
--------------
Bar
Bar

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-18 12:38 UTC] cmb@php.net
-Status: Open +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2016-06-18 12:38 UTC] cmb@php.net
Duplicate of bug #64963.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 10 12:01:27 2024 UTC