Question

Is there a way to check if an object has any fields? For example, I have a soap server I am querying using a soap client and if I call a get method, I am either returned an object containing fields defining the soap query I have made otherwise I am returned object(stdClass)#3 (0) { }.

Is there a way to tell if the object has anything?

    public function get($id){
    try{
        $client = new soapclient($this->WSDL,self::getAuthorization());
        $result = $client->__soapCall('get', array('get'=> array('sys_id'=>$id)));
        if(empty($result)){$result = false; }

    }catch(SoapFault $exception){
        //echo $exception;      
        $result = false;
    } 
    return $result;
}//end get() 

This method should return either an object or false and I am only receiving an object with no fields or an object with fields.

Was it helpful?

Solution

Updated to reflect current behavior, 5/30/12

empty() used to work for this, but the behavior of empty() has changed several times. As always, the php docs are always the best source for exact behavior and the comments on those pages usually provide a good history of the changes over time. If you want to check for a lack of object properties, a very defensive method at the moment is:

if (is_object($theObject) && (count(get_object_vars($theObject)) > 0)) {
    ...

OTHER TIPS

One of the user contributed code on the php empty() page which I think addresses your problem of checking if the array is filled but has empty values.

http://www.php.net/manual/en/function.empty.php#97772 To find if an array has nothing but empty (string) values:

<?php 
$foo = array('foo'=>'', 'bar'=>''); 
$bar = implode('', $foo); 

if (empty($bar)) { 
    echo "EMPTY!"; 
} else { 
    echo "NOT EMPTY!"; 
} 
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top