Determining if object property is empty

Viewed 20235

I feel like I'm missing something here. I've been using PHP's empty() function for quite a while now in determining if a variable is empty. I wanted to use it to determine if an object's property is empty, but somehow it doesn't work. Here's a simplified class to illustrate the problem

// The Class 
class Person{
    private $number;

    public function __construct($num){
        $this->number = $num;
    }

    // this the returns value, even though its a private member
    public function __get($property){
        return intval($this->$property);
    }
}

// The Code    
$person = new Person(5);

if (empty($person->number)){
    echo "its empty";
} else {
    echo "its not empty";
}

So basically, the $person object should have a value (5) in its number property. As you might have guessed, the problem is that php echoes "its empty". But it's not!!!

However, it does work if I store the property in a variable, then evaluate it.

So what would be the best way to determine if an object property is empty? Thank you.

3 Answers
Related