如何在PHP中检查stdClass对象是否为空? [英] How to check if stdClass object is empty or not in php?

查看:1074
本文介绍了如何在PHP中检查stdClass对象是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何检查PHP中的对象是否为空

Possible Duplicate:
How to Check if an Object is empty in PHP

我有这个空对象

Array (
    [cart_items] => stdClass Object
        (
        )
)

当我使用empty()/is_null()时,它不起作用.当我使用sizeof($object)时,它返回一个.

When I use empty()/is_null() , it doesn't work. When I use sizeof($object), it returns one.

如何检查?

推荐答案

先投射到数组

$tmp = (array) $object;
var_dump(empty($tmp));

原因是,对象是对象,没有空对象"的有用定义,因为那里有足够的类,仅包含方法,而没有属性.他们应该被视为空"吗?

The reason is, that an object is an object and there is no useful definition of "an empty object", because there are enough classes out there, that only contains methods, but no properties. Should they considered as "empty"?

这篇关于如何在PHP中检查stdClass对象是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆