有没有办法检查对象是否是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
}