Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check that an object is empty in PHP?

Tags:

object

php

People also ask

How do you check if an object is empty?

Using Object.If the length of the array is 0 , then we know that the object is empty.

How check JSON object is empty or not in PHP?

$x = (array)$obj; if (empty($x)) ... Or cast to an array and count() : if (count((array)$obj)) ...

Is NULL or empty PHP?

is_null() The empty() function returns true if the value of a variable evaluates to false . This could mean the empty string, NULL , the integer 0 , or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL .

Is empty PHP array?

An empty array is falsey in PHP, so you don't even need to use empty() as others have suggested. PHP's empty() determines if a variable doesn't exist or has a falsey value (like array() , 0 , null , false , etc).


You can cast to an array and then check if it is empty or not

$arr = (array)$obj;
if (!$arr) {
    // do stuff
}

Edit: I didn't realize they wanted to specifically check if a SimpleXMLElement object is empty. I left the old answer below

Updated Answer (SimpleXMLElement):

For SimpleXMLElement:

If by empty you mean has no properties:

$obj = simplexml_load_file($url);
if ( !$obj->count() )
{
    // no properties
}

OR

$obj = simplexml_load_file($url);
if ( !(array)$obj )
{
    // empty array
}

If SimpleXMLElement is one level deep, and by empty you actually mean that it only has properties PHP considers falsey (or no properties):

$obj = simplexml_load_file($url);
if ( !array_filter((array)$obj) )
{
    // all properties falsey or no properties at all
}

If SimpleXMLElement is more than one level deep, you can start by converting it to a pure array:

$obj = simplexml_load_file($url);
// `json_decode(json_encode($obj), TRUE)` can be slow because
// you're converting to and from a JSON string.
// I don't know another simple way to do a deep conversion from object to array
$array = json_decode(json_encode($obj), TRUE);
if ( !array_filter($array) )
{
    // empty or all properties falsey
}


Old Answer (simple object):

If you want to check if a simple object (type stdClass) is completely empty (no keys/values), you can do the following:

// $obj is type stdClass and we want to check if it's empty
if ( $obj == new stdClass() )
{
    echo "Object is empty"; // JSON: {}
}
else
{
    echo "Object has properties";
}

Source: http://php.net/manual/en/language.oop5.object-comparison.php

Edit: added example

$one = new stdClass();
$two = (object)array();

var_dump($one == new stdClass()); // TRUE
var_dump($two == new stdClass()); // TRUE
var_dump($one == $two); // TRUE

$two->test = TRUE;
var_dump($two == new stdClass()); // FALSE
var_dump($one == $two); // FALSE

$two->test = FALSE;
var_dump($one == $two); // FALSE

$two->test = NULL;
var_dump($one == $two); // FALSE

$two->test = TRUE;
$one->test = TRUE;
var_dump($one == $two); // TRUE

unset($one->test, $two->test);
var_dump($one == $two); // TRUE

You can cast your object into an array, and test its count like so:

if(count((array)$obj)) {
   // doStuff
}

Imagine if the object is not empty and in a way quite big, why would you waste the resources to cast it to array or serialize it...

This is a very easy solution I use in JavaScript. Unlike the mentioned solution that casts an object to array and check if it is empty, or to encode it into JSON, this simple function is very efficient concerning used resources to perform a simple task.

function emptyObj( $obj ) {
    foreach ( $obj AS $prop ) {
        return FALSE;
    }

    return TRUE;
}

The solution works in a very simple manner: It wont enter a foreach loop at all if the object is empty and it will return true. If it's not empty it will enter the foreach loop and return false right away, not passing through the whole set.