note 108311 deleted from function.class-uses by danbrown
| From: | danbrown@php.net | Date: | Thu, 10 May 2012 17:10:37 +0000 |
| Subject: | note 108311 deleted from function.class-uses by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187664@lists.php.net to get a copy of this message | ||
Note Submitter: josh at bitmagnet dot org
----
I made a slight mistake in my last note, the correct code is below.
Since traits can use other traits, the following function will recursively check if a class uses a
trait anywhere in the trait hierarchy.
<?php
function class_uses_recursive( $class, $trait )
{
$traits = class_uses( $class );
while( !empty( $traits ))
{
foreach( $traits as $k => $t )
{
if( $t == $trait )
return true;
if( class_uses_recursive( $t, $trait ))
return true;
unset( $traits[ $k ] );
}
}
return false;
}
?>