How can I check if a object is an instance of a specific 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:
- $var instanceof TestClass: The operator “instanceof” returns true if the variable $var is an object of the specified class (here is: “TestClass”).
- get_class($var): Returns the name of the class from $var, which can be compared with the desired class name.
- is_object($var): Checks whether the variable $var is an object.
Read more in How to check if an object is an instance of a specific class in PHP?