user991047
user991047

Reputation: 315

PHP Magic methods not working

I am trying to create a registry class with magic __set and __get my class looks like

  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.

here is the sample code how I am try to call this class

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

Upvotes: 0

Views: 435

Answers (2)

Coded Monkey
Coded Monkey

Reputation: 605

Your __get function doesn't return a value, that's why it's always null. It should be:

return $this->vars[$index];

Upvotes: 1

vsushkov
vsushkov

Reputation: 2435

  1. You forgot to return value in the __get method
  2. What does the dump function do?

Upvotes: 3

Related Questions