Not Set But Exists

../_images/not_set_but_exists.png

In this code, a custom class X is created. When checking the presence of a property a, isset() and empty() both say false. This is consistent with the definition of the class.

On the other hand, doing a comparison between the property and empty string, or null is also false: this looks paradoxical.

In fact, isset() and empty() both rely on the __isset() magic method. Since it is not created in the class, the default answer is false. Yet, the __get() method exists, and returns a non-empty value, so when reaching for the property directly with $x->a, a non-empty value is returned and compared to a falsy literal.

As a conclusion, it is important to provide __isset(), alongside the __get() method to be consistent when an object of the class is used.

This bug may also bite any code that relies on the convention to compare properties by value, instead of checking existence as a separate attribute.

See Also

PHP Features