【发布时间】:2019-09-01 01:30:59
【问题描述】:
我有一个类,它有一个我想在运行时取消设置/销毁的属性。取消设置发生在特定方法中,但调用它的方法在property_exists 中返回TRUE,而不能直接使用$this->property 访问属性,因为它返回通知Notice: Undefined property:...
public function get(int $id) {
if ($record->data) {
$this->_transform($record); // Calling method that unsets prop
}
if (! property_exists($this, 'isEmpty') ) { // FALSE
$this->transform();
}else{
echo $this->isEmpty; // FALSE as well!
}
return $this;
}
private method _transform(Record $record) {
unset($this->isEmpty); // Unsetting happens here
return;
}
正如您在取消设置后的代码中看到的那样,property_exists 返回不应发生的 TRUE,但该属性未定义。
编辑
似乎如果该属性在类的架构中声明,则它不能被销毁/取消设置(请参阅所选答案的演示),实际上它的行为自相矛盾:property_exists => TRUE, object->property => 警告
但是当属性未定义但在对象构造时创建时,则可以取消设置并按预期运行。
【问题讨论】: