1

Quote from the PHP documentation on overloading:

It is not possible to use overloaded properties in other language constructs than isset(). This means if empty() is called on an overloaded property, the overloaded method is not called.

To workaround that limitation, the overloaded property must be copied into a local variable in the scope and then be handed to empty().

Does this really mean that I cannot use something like

if (empty($this->foobar))

in a class, where $this->foobar is a magic property, resolved through __get(), or am I misunderstanding something here?

Community
  • 1
  • 1
Franz
  • 11,353
  • 8
  • 48
  • 70
  • I should add that I tested this and it *does* seem to work for me. – Franz Aug 30 '12 at 22:23
  • It resolves through `__isset()` firstly. – mario Aug 30 '12 at 22:30
  • 1
    possible duplicate of [How to implement __isset() magic method in PHP?](http://stackoverflow.com/questions/6242591/how-to-implement-isset-magic-method-in-php) – mario Aug 30 '12 at 22:31
  • @mario: So the fact that it works correctly means I implemented my `__isset()` method correctly!? – Franz Aug 30 '12 at 22:51

1 Answers1

3

It will work if you override the __isset method as well. The reason the documentation is written as such is because you can't call empty() or isset() on the result of a method call.

Lusitanian
  • 11,012
  • 1
  • 41
  • 38