是否可以在PHP中删除对象的属性? [英] Is it possible to delete an object's property in PHP?
本文介绍了是否可以在PHP中删除对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我说stdObject
,请说$a
.
当然,分配新属性$a
$a->new_property = $xyz;
但是我想删除它,所以unset
在这里没有帮助.
But then I want to remove it, so unset
is of no help here.
所以
$a->new_property = null;
是这样的.但是还有更优雅"的方式吗?
is kind of it. But is there a more 'elegant' way?
推荐答案
unset($a->new_property);
这适用于数组元素,变量和对象属性.
This works for array elements, variables, and object attributes.
示例:
$a = new stdClass();
$a->new_property = 'foo';
var_export($a); // -> stdClass::__set_state(array('new_property' => 'foo'))
unset($a->new_property);
var_export($a); // -> stdClass::__set_state(array())
这篇关于是否可以在PHP中删除对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文