Will isset() trigger __get and why?

前端 未结 5 1772
面向向阳花
面向向阳花 2021-01-02 05:33
class a {
   function __get($property){...}
}

$obj = new a();
var_dump(isset($obj->newproperty));

Seems the answer is nope but why?

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-02 06:16

    Because it checks __isset rather than retrieving it using __get.

    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.

提交回复
热议问题