我有一个对象被扔到会话数组中,我想运行 foreach
在 items 属性上。
我似乎无法访问它。我看到它是私有的,但我忍不住想知道为什么 var_dump 可以向我显示该属性包含的内容,但我无法读取数据,因为它会引发致命错误?
我想如果我真的需要的话,我可以做一些输出缓冲并将 var_dump 评估为字符串this但似乎应该有更好的方法。我有什么想法可以访问_items吗?
var_dumped 的目标代码var_dump($_SESSION['PHPurchaseCart'])
:
object(PHPurchaseCart)#191 (4) {
["_items:private"]=>
array(2) {
[0]=>
object(PHPurchaseCartItem)#190 (6) {
["_productId:private"]=>
string(2) "80"
["_quantity:private"]=>
int(1)
["_optionInfo:private"]=>
string(20) "Monthly Sponsorship "
["_priceDifference:private"]=>
string(3) ".01"
["_customFieldInfo:private"]=>
NULL
["_formEntryIds:private"]=>
array(0) {
}
}
[1]=>
object(PHPurchaseCartItem)#189 (6) {
["_productId:private"]=>
string(2) "75"
["_quantity:private"]=>
int(1)
["_optionInfo:private"]=>
string(20) "Monthly Sponsorship "
["_priceDifference:private"]=>
string(3) ".02"
["_customFieldInfo:private"]=>
NULL
["_formEntryIds:private"]=>
array(0) {
}
}
}
["_promotion:private"]=>
NULL
["_promoStatus:private"]=>
int(0)
["_shippingMethodId:private"]=>
NULL
}
我尝试访问它的方式:
$fun = $_SESSION['PHPurchaseCart'];
var_dump($fun->_items);
exit;
上面抛出了一个致命错误。
这就是私有财产的概念:你无法访问它们。你真的不应该打破这个概念。如果您确实想访问此类属性,请在原始类定义中将其标记为“public”。
var_dump之所以可以访问它,是因为它是一个内部函数,它有查看整个对象的“权力”。但是,您的代码没有这种能力。
我不推荐它,但如果你确实需要访问私有财产,你可以使用PHP 反射来实现它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)