PHP 对象已设置和/或为空

发布于 2024-09-13 21:10:16 字数 610 浏览 6 评论 0原文

有没有办法检查对象是否有任何字段?例如,我有一个肥皂服务器,我正在使用肥皂客户端进行查询,如果我调用 get 方法,我要么返回一个包含定义我所做的肥皂查询的字段的对象,否则我返回 object(stdClass)#3 (0 ){}。

有没有办法判断物体是否有东西?

    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() 

此方法应该返回一个对象或 false,我只接收一个不带字段的对象或一个带字段的对象。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆で 2024-09-20 21:10:16

已更新以反映当前行为,2012 年 5 月 30 日

empty() 曾经用于此目的,但empty() 的行为已更改多次。与往常一样,php 文档始终是准确行为的最佳来源,并且这些页面上的注释通常提供随时间变化的良好历史记录。如果你想检查是否缺少对象属性,目前一个非常防御性的方法是:

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

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)) {
    ...
望喜 2024-09-20 21:10:16

其中一位用户在 php empty() 页面上贡献了代码,我认为它解决了检查数组是否已填充但具有空值的问题。

http://www.php.net/manual/en/function。空.php#97772
要查找数组是否只有空(字符串)值:

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

if (empty($bar)) { 
    echo "EMPTY!"; 
} else { 
    echo "NOT EMPTY!"; 
} 
?>

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!"; 
} 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文