[英]PHP Magic methods not working
I am trying to create a registry class with magic __set
and __get
my class looks like 我正在尝试使用魔术
__set
创建一个注册表类,并且__get
我的类看起来像
class Registry {
private $vars = array();
public function __set($key, $value)
{
$this->vars[$key] = $value;
dump($key, $value);
}
public function __get($index)
{
$this->vars[$index];
}
}
but if i try to save some variable in registry class in gets only the $key
the $value
is alway NULL. 但是,如果我尝试在注册表类中保存一些变量,则仅获取
$key
则$value
始终为NULL。
here is the sample code how I am try to call this class 这是我尝试调用此类的示例代码
$registry = new registry;
$registry->router = $router;
$registry->title = "Welcome ";
__get
method __get
方法中返回值 dump
function do? dump
功能有什么作用? Your __get function doesn't return a value, that's why it's always null. 您的__get函数不返回值,这就是为什么它始终为null的原因。 It should be:
它应该是:
return $this->vars[$index];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.