__isset() Method
__isset() is a magic method: it is called when the existence of a property has to be checked.
That way, it is possible to create dynamically properties, without hardcoding them.
The __isset() method is usually paired with the __get() and __set() methods.
The __isset() is called by isset() and empty(). They are also called by the coalesce operator ?? and ??=. It is not called on null-safe object operator ?->.
<?php
class Elephpant {
private $values = ['a' => 1,
'b' => 2,
];
function __isset($name) {
return isset($this->values[$name]);
}
}
$elephpant = new Elephpant;
var_dump(isset($elephpant->a)); // true
var_dump(empty($elephpant->c)); // false
?>