PeeHaa
PeeHaa

Reputation: 72682

Check if property exists

Is it possible to check if a property exists which are set using magic setter?

class Test
{
    private $vars;

    public function __set($key, $value) {
        $this->vars[$key] = $value;
    }

    public function &__get($key)
    {
        return $this->vars[$key];
    }
}

$test = new Test;

$test->myvar = 'yay!';

if (magic_isset($test->myvar)) {
}

Or isn't it possible and I just need to setup another function in my class?

Upvotes: 3

Views: 12043

Answers (1)

user142162
user142162

Reputation:

Use __isset() and isset():

public function __isset($key)
{
    return isset($this->vars[$key]);
}
$test = new Test;

$test->myvar = 'yay!';

if (isset($test->myvar)) {

}

Upvotes: 7

Related Questions