php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #61554 ReflectionClass::getTraits does not return inherited traits
Submitted: 2012-03-29 16:52 UTC Modified: 2012-03-30 05:40 UTC
Votes:7
Avg. Score:3.1 ± 1.2
Reproduced:5 of 5 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: afredmyers at gmail dot com Assigned:
Status: Open Package: Reflection related
PHP Version: 5.4.0 OS: Ubuntu 11.10
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: afredmyers at gmail dot com
New email:
PHP Version: OS:

 

 [2012-03-29 16:52 UTC] afredmyers at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/reflectionclass.gettraits#refsect1-reflectionclass.gettraits-returnvalues
---
ReflectionClass::getTraits() does not return traits inherited from ancestor classes.

Test script:
---------------
trait Balding {
  public function loseHair(){
    echo get_class($this) . " is losing his hair!\n\n";
  }
}

class Father {
  use Balding;
}

class Son extends Father {}

$Son = new Son;
$Son->loseHair();

$Reflect = new ReflectionClass($Son);
print_r($Reflect->getTraits());

Expected result:
----------------
Son is losing his hair!

Array
(
  [Balding] => ReflectionClass Object
  (
    [name] => Balding
  )
)

Actual result:
--------------
Son is losing his hair!

Array
(
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-29 16:59 UTC] afredmyers at gmail dot com
-Type: Documentation Problem +Type: Bug
 [2012-03-29 16:59 UTC] afredmyers at gmail dot com
whoops, changed bug type from "Documentation Type" to "Bug"
 [2012-03-30 05:35 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2012-03-30 05:35 UTC] aharvey@php.net
The current behaviour is correct. Traits are outside the inheritance system: the 
parent class is effectively defined by the composition of its own 
methods/properties and any traits it uses, so trait usage is not actually 
inherited. Furthermore, it doesn't really make sense to reflect trait usage down 
the class hierarchy because the same trait may be used more than once within a 
class hierarchy.
 [2012-03-30 05:40 UTC] gron@php.net
-Status: Not a bug +Status: Open -Type: Bug +Type: Documentation Problem
 [2012-03-30 05:40 UTC] gron@php.net
Would be great if that ends up in the docs.

Thanks
Stefan
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 24 02:01:28 2024 UTC