【问题标题】:Is Empty and NULL the same in a comparision operation in PHP?PHP中比较运算符中的Empty和NULL是否相同?
【发布时间】:2016-01-04 11:03:13
【问题描述】:

我有一个类,该类有一个方法,该方法期望来自 API 服务的响应以数组格式。然后,此方法通过转换 (object)$response_array 将响应数组转换为对象。在此之后,该方法尝试解析对象的内容。返回的数组可能为空。在我的类方法中解析对象的内容之前,我会在 if...else 块中检查 null 或空对象。我想使用像if($response_object === null){} 这样的等价比较运算符,而不是if(empty($response_object)){}。 下面是我的班级的样子

<?php 
class ApiCall {

    //this method receives array response, converts to object and then parses object
    public function parseResponse(array $response_array)
    {
        $response_object = (object)$response_array;

        //check if this object is null
        if($response_object === null) //array with empty content returned
        {
          #...do something

        }
        else //returned array has content 
        {
           #...do something

        }

    }

}
?>

所以我的问题是 - 这是检查空对象的正确方法,而不使用函数 empty() 并且它是否一致?如果没有,那么我该如何修改此代码以获得一致的结果。这将帮助我了解 nullempty 在 PHP 对象中是否表示相同的意思。如果我仍然可以使用类似 ===

这样的等效比较,我将不胜感激

【问题讨论】:

标签: php arrays object operators is-empty


【解决方案1】:

这不是检查空对象的正确方法。如果您使用空数组调用函数 parseResponseif 条件仍然为 false。

所以,如果您将echo 放入if-else 代码中,如下所示:

class ApiCall {
    //this method receives array response, converts to object and then parses object
    public function parseResponse(array $response_array)
    {
        $response_object = (object)$response_array;
        //check if this object is null
        if($response_object === null) { // not doing what you expect
          echo "null";
        }
        else {
          echo "not null";
        }
    }
}

然后这个调用:

ApiCall::parseResponse(array()); // call with empty array

...将输出

不为空

如果您测试empty($response_object),也会发生同样的情况。这在遥远的过去曾经有所不同,但从 PHP 5.0(2004 年中)开始,没有属性的对象不再被视为空。

你应该只测试你已经拥有的数组,当它为空时是错误的。所以你可以写:

        if(!$response_array) {
          echo "null";
        }
        else {
          echo "not null";
        }

或者,如果您真的想要(不)平等,那么请使用$response_array == false,确保使用== 而不是===。但就我个人而言,我发现这种与布尔文字的比较只不过是浪费空间。

对于if 条件,以下所有选项都是可行的:

基于$response_array

!$response_array
!count($response_array)
count($response_array) === 0
empty($response_array)

基于$response_object

!get_object_vars($response_object)
!(array)($response_object)

请注意,如果 $response_object 不是标准对象并且具有继承属性,get_object_vars 可能会给出与数组转换方法不同的结果。

【讨论】:

  • 这听起来很有用。所以从技术上讲,我得到的是我无法通过使用 nullempty() 来始终测试空对象。我必须先测试有问题的array(),然后再转换为(object)。不过,这对于我的应用程序逻辑来说听起来很棘手——让我想想这是否是我想要做的。感谢您的回复。
  • 是的,投射前的测试是最简单的。但是,当您仍然可以访问原始数组进行测试时,没有什么能阻止您先进行投射。但是,如果您已经将数组转换为对象并且此时无法再测试原始数组(出于某些应用程序逻辑原因),那么请选择最后提到的选项之一:!get_object_vars($response_object)!(array)($response_object)。我觉得两者中的第一个更具可读性。
【解决方案2】:

看看这个例子

$ php -a
php > $o = (object)null;
php > var_dump($o);
class stdClass#2 (0) {
}
php > var_dump(!$o);
bool(false)

因此,在您的情况下将 object 与 null 进行比较并不是一个好主意。更多信息:How to check that an object is empty in PHP?

【讨论】:

  • 这段代码应该在终端测试吧?
猜你喜欢
  • 2010-12-20
  • 2011-06-11
  • 2023-04-08
  • 1970-01-01
  • 2014-07-31
  • 1970-01-01
  • 1970-01-01
  • 2015-07-02
  • 2016-04-28
相关资源
最近更新 更多