PHP Recipients / Setters

I am trying to learn this MVC OOP thing, and I came across a strange error:

Fatal error: Call to undefined method Foo::stuff() in ... 

The code I have is:

 class Foo extends FooBase{ static $_instance; private $_stuff; public function getStuff($which = false){ if($which) return self::app()->_stuff[$which]; else return self::app()->_stuff; } public function setStuff($stuff){ self::app()->_stuff = $stuff; } public static function app(){ if (!(self::$_instance instanceof self)){ self::$_instance = new self(); } return self::$_instance; } } Foo::app()->stuff = array('name' => 'Foo', 'content' => 'whatever'); echo Foo::app()->stuff('name'); // <- this doesn't work... 

The FooBase class is as follows:

 class FooBase{ public function __get($name){ $getter = "get{$name}"; if(method_exists($this, $getter)) return $this->$getter(); throw new Exception("Property {$name} is not defined."); } public function __set($name, $value){ $setter = "set{$name}"; if(method_exists($this, $setter)) return $this->$setter($value); if(method_exists($this, "get{$name}")) throw new Exception("Property {$name} is read only."); else throw new Exception("Property {$name} is not defined."); } } 

So, if I understand correctly, the getter function cannot have arguments? What for? Or am I doing something wrong here?

+4
source share
1 answer

Everything with ellipses is considered as a method. The __get and __set magic methods work only for things that look like properties.

For method magic see __call() .

+4
source

All Articles