Hướng dẫn check object empty php

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: //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

Object Initialization

To create a new object, use the new statement to instantiate a class:

For a full discussion, see the Classes and Objects chapter.

Converting to object

If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was null, the new instance will be empty. An array converts to an object with properties named by keys and corresponding values. Note that in this case before PHP 7.2.0 numeric keys have been inaccessible unless iterated.

For any other value, a member variable named scalar will contain the value.

helpful at stranger dot com

10 years ago

By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose:



I had the most difficult time finding this, hopefully it will help someone else!

Anthony

6 years ago

In PHP 7 there are a few ways to create an empty object:



$obj1 and $obj3 are the same type, but $obj1 !== $obj3. Also, all three will json_encode[] to a simple JS object {}:



Outputs: [{},{},{}]

Ashley Dambra

8 years ago

Here a new updated version of 'stdObject' class. It's very useful when extends to controller on MVC design pattern, user can create it's own class.

Hope it help you.


works and displays:
stdClass Object
[
    [a] => A
    [b] => B
    [0] => C
]

But this:


or

Chủ Đề