2017-12-22 11 views

답변

2

당신은 getTraits 또는 getTraitNames 기능 ReflectionObject를 사용할 수 있습니다

trait test { 
    public function hello() 
    { 
     echo "hello"; 
    } 
} 

class A { 
    use test; 
} 

function hasTrait($object, $traitName) 
{ 
    $reflection = new ReflectionObject($object); 

    return in_array($traitName, $reflection->getTraitNames()); 
} 

$a = new A(); 

if(hasTrait($a, 'test')) { 
    echo "Object of class 'A' has 'test' trait \n"; 
}