Tuesday, 29 November 2016

php - Why does empty() not accept function return value when it accepts getter output?




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.


Answer



empty in php version less than 5.5 accepts only variables.



from changelog:





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



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...