PHP Magic方法不起作用

时间:2011-10-22 10:34:29

标签: php

我正在尝试使用魔术__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];
      }
  } 

但如果我尝试在注册表类中保存一些变量只获取$key $value 总是空的。

以下是我尝试调用此类的示例代码

$registry   = new registry; 
$registry->router = $router; 
$registry->title = "Welcome ";

2 个答案:

答案 0 :(得分:3)

  1. 您忘记在__get方法
  2. 中返回值
  3. dump功能有什么作用?

答案 1 :(得分:1)

你的__get函数没有返回值,这就是为什么它始终为null。它应该是:

return $this->vars[$index];

相关问题