dthy81285
2017-11-25 20:19 浏览 16
已采纳

如果对象为0,如何检查对象是否为空

$view = new UserView();
var_dump(is_null($view))
echo '<br>';
var_dump($view);

bool(false)

object(UserView)#2 (0) { }

How is this even possible? And how I check if the object is not null?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答 默认 最新

  • 已采纳
    douzhaishan5462 douzhaishan5462 2017-11-25 20:21

    Your object is not null, it's just empty, try :

     $empty = (bool) count((array) $view);
    
    点赞 评论 复制链接分享
  • doudu9094 doudu9094 2017-11-25 20:27

    Try the function get_object_vars https://secure.php.net/manual/en/function.get-object-vars.php

    array get_object_vars ( object $object ) Gets the accessible non-static properties of the given object according to scope.

    $view = new stdclass();
    echo count(get_object_vars($view));
    
    点赞 评论 复制链接分享

相关推荐