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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2021 The PHP Group
All rights reserved.
Last updated: Wed Dec 08 10:03:35 2021 UTC