如何检查对象是否为特定类的实例?
有没有办法检查对象是否为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);
}
}
}
这是我的函数,仅当提供的对象是SimpleXMLElement时,$role->xpath才可能。有人吗?
解决方案
可以使用instanceof检查对象是否是类的实例,例如
if($role instanceof SimpleXMLElement) {
//do stuff
}
相关文章