小编典典

是否可以在PHP中删除对象的属性?

php

如果我有stdObject发言权,$a

当然,分配新的属性没有问题$a

$a->new_property = $xyz;

但是之后我想将其删除,因此unset这里无济于事。

所以,

$a->new_property = null;

是这样的。但是,还有一种更“优雅”的方式吗?


阅读 370

收藏
2020-05-29

共1个答案

小编典典

unset($a->new_property);

这适用于数组元素,变量和对象属性。

例:

$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())
2020-05-29