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?
an object is an element (or instance) of a class; objects have the behaviors of their class. The object is the actual component of programs, while the class specifies how instances are created and how they behave.
The isinstance() method checks whether an object is an instance of a class whereas issubclass() method asks whether one class is a subclass of another class (or other classes).
A class can create objects of itself with different characteristics and common behaviour. So, we can say that an Object represents a specific state of the class. For these reasons, an Object is called an Instance of a Class.
You can check if an object is an instance of a class with instanceof
, e.g.
if($role instanceof SimpleXMLElement) { //do stuff }
The following methods and operators are useful to determine whether a particular variable is an object of a specified class:
Read more in How to check if an object is an instance of a specific class in PHP?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With