Definition and Usage
- PHP Version
- 4+
The is_object() function checks whether a given value is an object.
It returns true if the value passed as an argument is an object; otherwise, it returns false.
The object type is a data type representing an object.
In PHP, an object is created from a class, and the class defines the properties and methods of the object. An object, as an instance of such a class, contains both data and behavior.
Basic Example
class MyClass {
public $name = 'John Doe';
public $age = 30;
}
$obj1 = new MyClass();
$obj2 = clone $obj1;
/* Classes */
var_dump(is_object($obj1)); // bool(true)
var_dump(is_object($obj2)); // bool(true)
/* Arrays */
var_dump(is_object(array(1, 2, 3))); // bool(false)
var_dump(is_object([1, 2, 3])); // bool(false)
var_dump(is_object([])); // bool(false)
/* Strings and Numbers */
var_dump(is_object('Hello World!')); // bool(false)
var_dump(is_object(7)); // bool(false)
var_dump(is_object(0.13)); // bool(false)
/* Booleans */
var_dump(is_object(true)); // bool(false)
/* null */
var_dump(is_object(null)); // bool(false)
/* Type Casting */
var_dump(is_object((object) 1)); // bool(true) -> Casting an integer to an object
var_dump(is_object((object) 1.9)); // bool(true) -> Casting a float to an object
var_dump(is_object((object) true)); // bool(true) -> Casting a boolean to an object
Note:
The gettype() function returns the data type of the given value as a string.
Syntax
is_object(mixed $value): bool
Parameters
$value |
Required. The value to check. |
|---|
Return Values
If the value passed as an argument is an object, the function returns true; otherwise, it returns false.
Changes as of PHP 7.2.0
As of PHP 7.2.0, the behavior of the is_object() function has changed. Previously, for serialized objects without a class definition (referring to the __PHP_Incomplete_Class class), the is_object() function returned false. However, starting from PHP 7.2.0, this has been changed to return true in such cases.
Things to Keep in Mind
There are several important points to be aware of when using the is_object() function.
Empty Objects Still Return true
The is_object() function returns true for any variable that refers to an object, including empty objects. This is because the is_object() function simply checks whether a given variable is of the object type. In other words, an empty object is still recognized as an object and thus returns true.
// Creating an empty object
$empty_object = new stdClass();
var_dump(is_object($empty_object)); // bool(true)
Arrays are Not Considered Objects
While arrays can function in ways similar to objects, they are not the same. Objects offer significantly more functionality than arrays and possess various characteristics that operate differently. Therefore, it is important to remember that the is_object() function will always return false when checking an array.
$arr = array(1, 2, 3);
var_dump(is_object($arr)); // bool(false)
To check if a variable's type is an array, it is recommended to use the is_array() function.
is_array() function to check if the type is an array
$arr = array(1, 2, 3);
var_dump(is_array($arr)); // bool(true)
Practical Examples
The is_object() function is convenient and useful in the following scenarios.
To Enhance Code Reliability
When you need to verify if a specific variable is an object within your code, you can use the is_object() function to perform a safe check. This prevents potential errors that may occur when variables of unexpected types are passed.
function my_function($data) {
if (!is_object($data)) {
throw new InvalidArgumentException('The argument must be an object.');
}
// ...
}
In the code above, my_function() requires an argument named $data. If data of a type other than an object is passed when the function is called, it throws an exception to prevent the function from behaving abnormally.
Before Performing Object-Related Operations
It is good practice to use the is_object() function to verify that a variable is actually an object before performing operations related to objects. This helps prevent errors and improves code readability.
$data = maybe_get_object();
if (is_object($data)) {
foreach ($data as $property => $value) {
// Perform operations on each property of the object
}
}
Code Explanation
The foreach() loop is a fundamental construct used to iterate over arrays and objects for repetitive processing.
In this code, we assume the $data variable holds a value returned by the maybe_get_object() function. Since the returned value might not be an object, the is_object() function is used to verify the type of $data.
References
See also
- PHP gettype() Function – Get the Data Type of a Variable as a String
- PHP is_int() Function – Checking Whether a Value Is an Integer
- PHP is_float() Function – Checking Whether a Value Is a Float
- PHP is_numeric() Function – Checking Whether a Value Is a Number or a Numeric String
- PHP is_string() Function – Checking Whether a Value Is a String
- PHP is_array() Function – Checking Whether a Value Is an Array
- PHP is_bool() Function – Checking Whether a Value Is a Boolean
- PHP is_resource() Function – Checking Whether a Value Is a Resource
- PHP is_null() Function – Checking Whether a Value Is NULL
- PHP isset() and empty() Functions – Concepts, Usage, and Key Differences
- PHP Superglobals – Simple and Essential Usage Guide