php – 检查属性是否存在
作者:互联网
是否可以检查是否存在使用魔术设定器设置的属性?
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)) {
}
或者不可能,我只需要在班上设置另一个功能?
解决方法:
public function __isset($key)
{
return isset($this->vars[$key]);
}
$test = new Test;
$test->myvar = 'yay!';
if (isset($test->myvar)) {
}
标签:php,getter-setter 来源: https://codeday.me/bug/20190823/1696416.html