1

I have a class that has a method that expects a response from an API service in array format. This method then converts the response array into an object by casting (object)$response_array. After this the method attempts to parse the contents of the object. There is a possibility that the returned array could be empty. Before parsing the contents of the object in my class method, I perform a check for null or empty object in an if...else block. I would like to use an equivalence comparison operator like if($response_object === null){} and not if(empty($response_object)){}. Below is how my class looks like

<?php 
class ApiCall {

    //this method receives array response, converts to object and then parses object
    public function parseResponse(array $response_array)
    {
        $response_object = (object)$response_array;

        //check if this object is null
        if($response_object === null) //array with empty content returned
        {
          #...do something

        }
        else //returned array has content 
        {
           #...do something

        }

    }

}
?>

So my question is - is this the right way to check for empty object, without using the function empty() and is it consistent? If not then how can I modify this code to get consistent results. This would help me know if null and empty mean the same thing in PHP objects. I would appreciate any answer where I can still use an equivalent comparison like this ===

2 Answers2

2

It is not the right way to check for an empty object. If you call your function parseResponse with an empty array, the if condition will still be false.

So, if you would put echo in the if-else code like this:

class ApiCall {
    //this method receives array response, converts to object and then parses object
    public function parseResponse(array $response_array)
    {
        $response_object = (object)$response_array;
        //check if this object is null
        if($response_object === null) { // not doing what you expect
          echo "null";
        }
        else {
          echo "not null";
        }
    }
}

Then this call:

ApiCall::parseResponse(array()); // call with empty array

... will output

not null

The same happens if you test for empty($response_object). This used to be different in a distant past, but as from PHP 5.0 (mid-2004), objects with no properties are no longer considered empty.

You should just test on the array you already have, which is falsy when empty. So you can just write:

        if(!$response_array) {
          echo "null";
        }
        else {
          echo "not null";
        }

Or, if you really want an (in)equality, then do $response_array == false, making sure to use == and not ===. But personally, I find such comparisons with boolean literals nothing more than a waste of space.

All of the following would be working alternatives for the if condition:

Based on $response_array:

!$response_array
!count($response_array)
count($response_array) === 0
empty($response_array)

Based on $response_object:

!get_object_vars($response_object)
!(array)($response_object)

Note that get_object_vars could give a different result than the array cast method if $response_object were not a standard object, and would have inherited properties.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • This sounds useful. So technically what I am getting is that i cannot consistently test for an empty object by using either `null` nor `empty()` . And that I have to first test the `array()` in question before casting to `(object)`. This sounds tricky for my application logic though - let me think out if that is what I would want to do. Thanks for your response. – geoffreybans Jan 04 '16 at 13:26
  • 1
    Yes, testing before casting would be the easiest. But nothing stops you from casting first, when you still have access the original array to test with. If however, you have already cast your array to an object and cannot test the original array any more at that point (for some application logic reason), then go for one of the last mentioned options: `!get_object_vars($response_object)` or `!(array)($response_object)`. I feel the first of the two is the more readable one. – trincot Jan 04 '16 at 14:11
1

Look at this example

$ php -a
php > $o = (object)null;
php > var_dump($o);
class stdClass#2 (0) {
}
php > var_dump(!$o);
bool(false)

So, it is not good idea to compare object with null in your case. More about this: How to check that an object is empty in PHP?

Community
  • 1
  • 1
Nick
  • 9,735
  • 7
  • 59
  • 89