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 ===