1

I'm having a class that implements the ArrayAccess interface. I noticed that I can use empty function on the offset values with no errors:

$class = new MyArrayClass();
if(!empty($class["offset"])) 
   ...
else
   die("Empty!!!");

However calling even the offsetGet interface method will not work:

if(!empty($class->offsetGet("offset"))) 

It throws standard error:

Can't use function return value in write context.

My question is: Why does empty work on getters and virtual array offsets? As far as I know, they are actually function return values, not variables...

This question is rather educational then practical. I'm just curious. Please try to explain as much as possible.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Please see : http://stackoverflow.com/questions/1075534/cant-use-method-return-value-in-write-context/4328049#4328049 – Spoke44 Jan 15 '15 at 09:58
  • Note that this will work fine if you use PHP 5.5+ (with a caveat on the `E_NOTICE`): http://3v4l.org/bG17l – cmbuckley Jan 15 '15 at 10:00

1 Answers1

1

empty in php version less than 5.5 accepts only variables.

from changelog:

5.5.0 empty() now supports expressions, rather than only variables.