简体   繁体   English

PHP Magic方法不起作用

[英]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 ";
  1. You forgot to return value in the __get method 您忘记了在__get方法中返回值
  2. What does the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM