脚本宝典收集整理的这篇文章主要介绍了php – 如何检查对象是否为空?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
5.0.0 Objects wITh no PRoPErties are no longer considered empty.
http://www.php.net/manual/en/reflectionclass.getproperties.php
class A { public $p1 = 1; protected $p2 = 2; private $P3 = 3; } $a = new A(); $a->newProp = '1'; $ref = new ReflectionClass($a); $props = $ref->getProperties(); // Now you can use $props with empty echo empty($props); print_r($props); /* output: Array ( [0] => ReflectionProperty Object ( [name] => p1 [class] => A ) [1] => ReflectionProperty Object ( [name] => p2 [class] => A ) [2] => ReflectionProperty Object ( [name] => p3 [class] => A ) ) */
请注意,newProp不会在列表中返回.
get_object_VARs
http://php.net/manual/en/function.get-object-vars.php
使用get_object_vars将返回newProp,但不会返回受保护和私有成员.
因此,根据您的需要,可能需要反射和get_object_vars的组合.
以上是脚本宝典为你收集整理的php – 如何检查对象是否为空?全部内容,希望文章能够帮你解决php – 如何检查对象是否为空?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。