What is the is_object method in PHP?
The is_object method can be used to check if the variable is an object or not.
Syntax
is_object(mixed $value): bool
The is_object method returns true if the passed value is an object. Otherwise, it returns false.
Code
<?phpclass MyClass {}$test = new myClass();printIsObject($test);$test = new stdClass();printIsObject($test);$test = null;printIsObject($test);$test = 0;printIsObject($test);function printIsObject($test){echo "The Value is: ";var_dump($test);echo "Is Object: ";var_dump(is_object($test));echo "---------\n\n";}?>
Explanation
In the code above:
-
We create an empty class with the name
MyClassand create an object for it. We pass the created object to theis_objectmethod, which returnstrue. -
Similarly, we create an object for
stdClassand callis_objectwith the created object as an argument. -
For the values
nulland0(int), theis_objectmethod returnsfalsebecause they are not objects.