是否可以在 PHP 中删除对象的属性? [英] Is it possible to delete an object's property in PHP?
本文介绍了是否可以在 PHP 中删除对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个 stdObject
说,$a
.
If I have an stdObject
say, $a
.
当然分配一个新的属性没有问题,$a
,
Sure there's no problem to assign a new property, $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屋!
查看全文