Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP object isset and/or empty

Tags:

object

php

isset

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.

like image 348
Chris Avatar asked Aug 10 '10 14:08

Chris


1 Answers

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)) {
    ...
like image 62
Kevin Vaughan Avatar answered Sep 23 '22 03:09

Kevin Vaughan