Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检查对象是否是特定类的实例?_Php_Simplexml - Fatal编程技术网

Php 如何检查对象是否是特定类的实例?

Php 如何检查对象是否是特定类的实例?,php,simplexml,Php,Simplexml,是否有方法检查对象是否为simplexmlement private function output_roles($role) { foreach ($role as $current_role) { $role_ = $current_role->attributes(); $role_type = (string) $role_->role; echo "<tr>"; echo "<td&g

是否有方法检查对象是否为
simplexmlement

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);
        }
    }
}
private函数输出\u角色($role){
foreach($role作为$current\u角色){
$role_u=$current_role->attributes();
$role\u type=(字符串)$role\u->role;
回声“;
回显“$role_type.”;
回声“;
$roles=$role->xpath('//role[@role=“Administrator”]//role[not(role)]);
if(is_数组($roles)){
$this->output_角色($roles);
}
}
}

这是我的函数,
$role->xpath
只有在提供的对象是
simplexmlement
时才可能使用。有人吗?

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


以下方法和运算符可用于确定特定变量是否为指定类的对象:

  • $var instanceof TestClass:如果 变量$var是指定类的对象(如下所示: “测试类”)
  • 获取类($var):从$var返回类的名称,可以 与所需的类名进行比较
  • 是对象($var):检查变量$var是否是对象
请在

中阅读更多可能的副本
if($role instanceof SimpleXMLElement) {
    //do stuff
}