isset(), empty() And the Magic Methods

../_images/isset-empty-valued.png

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

PHP Features