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
}