Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

is_object
php
object
communitycreator

What is the is_object method in PHP?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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

<?php
class 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 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.

RELATED TAGS

is_object
php
object
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring