How can I check if a object is an instance of a specific class?

ⅰ亾dé卋堺 提交于 2019-12-19 06:21:22

问题


Is there a way to check if an object is an 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);
        }
    }
}

This is my function and the $role->xpath is only possible if the provided object is a SimpleXMLElement. Anyone?


回答1:


You can check if an object is an instance of a class with instanceof, e.g.

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


来源:https://stackoverflow.com/questions/30331322/how-can-i-check-if-a-object-is-an-instance-of-a-specific-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!