The is_object
method can be used to check if the variable is an object
or not.
is_object(mixed $value): bool
The is_object
method returns true
if the passed value is an object
. Otherwise, it returns false
.
<?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";}?>
In the code above:
We create an empty class with the name MyClass
and create an object for it. We pass the created object to the is_object
method, which returns true
.
Similarly, we create an object for stdClass
and call is_object
with the created object as an argument.
For the values null
and 0(int)
, the is_object
method returns false
because they are not objects.