我正在使用一个类,该类具有一个要在运行时取消/销毁的属性。该解封发生在一个特定的方法,但该方法调用它返回TRUE的property_exists,虽然它不能直接访问属性$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; }正如您在取消设置后的代码中看到的那样,property_exists返回TRUE不应发生,但该属性是未定义的。编辑似乎如果该属性是在类的架构中声明的,则它无法被破坏/取消设置(请参阅所选答案的演示),并且实际上它的行为自相矛盾:property_exists => TRUE,object-> property => warning但是,如果未定义属性而是在对象的构造中创建了该属性,则可以将其取消设置并按预期方式运行。
1 回答
![?](http://img1.sycdn.imooc.com/5458631e0001ffd402200220-100-100.jpg)
largeQ
TA贡献2039条经验 获得超7个赞
从PHP 5.3.0开始,如果将其定义为对象变量,则即使在之后也property_exists
返回。请改用,因为它会在之后返回。true
unset
isset($this->isEmpty)
false
unset
但是,您可能应该采用其他方法,例如设置为true
orfalse
或null
or并进行检查。
- 1 回答
- 0 关注
- 172 浏览
添加回答
举报
0/150
提交
取消