I've written my own ORM that returns user objects from an MySQL DB.
I've got this weird glitch with empty to check a returned value. Essentially, I need to print 'Yes' or 'No' based on the value. Very simple.
<?=( empty($user->flagOnUnemploymentStatusAllowance) ) ? 'No' : 'Yes' ?>
Nothing crazy there. However, regardless of the value returned from DB, it always prints 'No'. Turns out empty() is always returning TRUE, even though the variable is NOT empty :
var_dump( $user->flagOnUnemploymentStatusAllowance, empty($user->flagOnUnemploymentStatusAllowance) );
// returns string(1) "1" bool(true)
The possible returned values are "0" or "1", as strings. What am I doing wrong here ? I can always change my empty() with a test flagOnUnemploymentStatusAllowance == '0'
, but find this stupid when empty() should do it, and catch nulls as well.
Thanks.
EDIT 1: Solution ? Since the properties are fetched with magic getters : I added the following function to my ORM Base class (where my magic lives):
public function __isset(string $property){
return empty($this->entity[$property]['value']) ? 'true' : 'false';
}
For some reasons, I must be tired, I had to use a ternary operator to force a STRING value of true / false ... It now seems to work if I try empty($user->flagOnUnemploymentStatusAllowance), BUT I don't understand it at all here! Can anyone shed a light on this one!?