php - 检查属性是否存在

标签 php getter-setter

是否可以检查是否存在使用 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)) {
}

或者这是不可能的,我只需要在我的类(class)中设置另一个功能?

最佳答案

使用__isset()isset() :

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

$test->myvar = 'yay!';

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

}

关于php - 检查属性是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8763357/

相关文章:

php - 从 Controller Symfony2 中的 URL 获取参数

PHP/或 mysql 搞乱了我的插入

php - 有没有比 curl 更好的从其他网站获取信息的方法?

javascript - 返回非值属性

Java:在创建父类(super class)类型的对象时使用子类的setter

php - PDO连接登录

php - 用jquery返回json数据?

apache-flex - 弹性 : Help me understand data Binding on getters and setters

java - 使用 IntelliJ IDEA (Java) 在文件末尾插入 Getter/Setter

java - 在 NetBeans 中生成 getter 和 setter