Reputation: 3457
Say,
$obj = $this->someFunc(); // this returns an object
if(empty($obj)){
// suppose $obj is null, it does works correctly
}
In http://php.net/manual/en/function.empty.php, empty()
is only used for variables and arrays.
But, is it the correct way?
Upvotes: 4
Views: 16813
Reputation: 116
Just check:
if ($obj === null) {
// Object is null
} else {
// Object isn't null
}
Which is also possible to do with:
if (is_null($obj)) {
// Object is null
}
Upvotes: 2
Reputation: 659
An object with no properties are not empty anymore as of PHP 5 I use isset a lot to chek that it has a value and that that value isnt NULL
Upvotes: 0
Reputation: 754
php has the function is_null()
to determine whether an object is null or not: http://php.net/manual/en/function.is-null.php
Upvotes: 11
Reputation: 219804
null
will cause empty()
to return true. However, if you're checking to see if that value is actually null, is_null()
is better suited for the job.
Upvotes: 3