Req #68742 [Opn->Dup]: ReflectionProperty::getDeclaringClass doesn't work with traits
| From: | cmb@php.net | Date: | Sat, 18 Jun 2016 12:38:06 +0000 |
| Subject: | Req #68742 [Opn->Dup]: ReflectionProperty::getDeclaringClass doesn't work with traits | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-201710@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68742&edit=1
ID: 68742
Updated by: cmb@php.net
Reported by: enumag at gmail dot com
Summary: ReflectionProperty::getDeclaringClass doesn't work
with traits
-Status: Open
+Status: Duplicate
Type: Feature/Change Request
Package: Reflection related
Operating System: any
PHP Version: 5.6.4
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Duplicate of bug #64963.
Previous Comments:
------------------------------------------------------------------------
[2015-01-04 14:12:48] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68742&edit=1