views:

32

answers:

2

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.

A: 

empty() used to work for this, but as of PHP5 this is no longer the case (see the documentation for empty() for a note on this). If you want to check for a lack of object properties, you can use:

if (empty(get_object_vars($theObject))) {
    ...
Kevin
That does the trick.
Chris
A: 

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!"; 
} 
?>
Lyon
I am not sure if it makes a difference but I am dealing with objects not arrays ?
Chris