views:

57

answers:

1
+1  Q: 

empty() behavior

Maybe I'm asking a stupid question, but I can't understand this behavior:

<?php
    $this->meeting->google_id = 'test';
    $test = $this->meeting->google_id;
    var_dump(empty($test));
    var_dump(empty($this->meeting));
    var_dump(empty($this->meeting->google_id));
?>

gives output:

bool(false) bool(false) bool(true) 

Why the result of empty($this->meeting->google_id); is true? And how should I check this property then?

+3  A: 

Read here: http://www.php.net/manual/en/function.empty.php#93117

Basically, PHP magic methods resulting in unexpected behavior.

You can read/write to virtual members in a class if the class has a special __get magic method. The actual value, however, cannot be checked by the __isset magic method (which is what empty uses), because it's not an explicit member of the class.

kurige
In case you didn't finish reading (which some people have a tendency not to do): you can always write your own `__isset` method, which the same post most courteously provides. :)
animuson
+1 for mentioning the __isset magic method... clearly I never finished reading the magic methods page past __get, __set and __call ;-)
richsage