isset(), empty() And the Magic Methods¶
<?php
class x {
private array $config = array();
function __isset($name) {
print __METHOD__." ".$name.PHP_EOL;
return isset($this->config[$name]);
}
function __get($name) {
print __METHOD__." ".$name.PHP_EOL;
return 'a';
}
}
$x = new x;
var_dump(isset($x->a));
var_dump(empty($x->a));
The __isset() and __get() methods must go hand in hand. One checks that a property, virtual or not, is available, and the other one returns the actual value. Yet, both may be inconsistent one with the other.
In particular, __isset() is called when dealing with isset() and empty().
But __get() may be called directly, and return something, even if __isset() is saying it’s not set.
This might come as a surprise to pieces of code that compare a property with == '' (or equivalent), compared to using isset().
Beyond the strange illustration, it is probably a good practice to always provide __isset() on a class that has the magic method __get.
See Also¶
isset, empty and get are on a boat [Try me]
PHP Features¶
Last updated: 14 July 2026