开发者

PHP object isset and/or empty

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开发者_如何学Python());
        $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.


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)) {
    ...


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!"; 
} 
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜