2015-05-19 101 views
1

有没有办法检查一个对象是否是SimpleXMLELement如何检查对象是否是特定类的实例?

private function output_roles($role) { 
    foreach ($role as $current_role) { 
     $role_ = $current_role->attributes(); 
     $role_type = (string) $role_->role; 
     echo "<tr>"; 
     echo "<td><b>" . $role_type . "</b></td>"; 
     echo "</tr>"; 
     $roles = $role->xpath('//role[@role="Administrator"]//role[not(role)]'); 
     if (is_array($roles)) { 
      $this->output_roles($roles); 
     } 
    } 
} 

这是我的功能和$role->xpath只可能是,所提供的对象是SimpleXMLElement。任何人?

+0

可能重复的[如何在PHP中检查特定类型的对象](http://stackoverflow.com/questions/8091143/how-to-check-for-a-specific-type-of -object-in-php) – IMSoP

回答

7

您可以检查对象是否为instanceof的类的实例,例如,

if($role instanceof SimpleXMLElement) { 
    //do stuff 
} 
+0

谢谢,效果很好。 – Snickbrack

+0

@Snickbrack不客气。 – Rizier123

相关问题