isset()会触发__get吗,为什么? [英] Will isset() trigger __get and why?
本文介绍了isset()会触发__get吗,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class a {
function __get($property){...}
}
$obj = new a();
var_dump(isset($obj->newproperty));
似乎答案不是,但是为什么呢?
Seems the answer is nope but why?
推荐答案
Because it checks __isset rather than retrieving it using __get.
调用__isset是更好的选择,因为对于空值没有标准.也许在类的上下文中,null是一个可接受的值.您还可以拥有一个类,如果该成员不存在,它将返回一个新的空对象,该对象将破坏isset($myObj->item)
,因为在这种情况下它将始终返回true.
It is a much better option to call __isset, as there is no standard on what is empty. Maybe in the context of the class null is an acceptable value. You could also have a class that if the member didn't exist, it returned a new empty object, which would break isset($myObj->item)
as in that case it would always return true.
这篇关于isset()会触发__get吗,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文