tags:

views:

64

answers:

3

Having the following code

class test {
    private $name;
    public function __get($name){
        return $name;
    }
    public function __set($name,$value){
        $this->name = $value;
    }
}
$obj = new test();
$obj->a = 2;

if (!empty($obj->a)) {
    echo 'not empty';
}

This is calling __isset. But this is not being defined so it always return empty. What is the best way to check for a non empty property?

Update :changing the class is not a solution because it's a 3th party component and it has to remain intact.

A: 

It does not make sense when used with anything other than the variable; ie empty (addslashes ($ name)) does not make sense, since it will be checked by anything other than a variable as a variable with a value of FALSE.

In your case, you should use the type conversion:

if ((bool)$obj->a) { echo 'not empty'; }

makedon
I doesn't work if that contains and empty string.
A: 

change

public function __set($name,$value){
        $this->name = $value;
    }
 To

public function __set($name,$value){
        $this->$name = $value;
    }

And then try

Yogesh
I made an update.Can't change the class.
+1  A: 

If you can't change the class, I think the only possible workaround is using a temporary variable.

$obj->a = 2;

$test = $obj->a; 

if (!empty($test)) {
    echo 'not empty';
}
Pekka
I was afraid of this.