PHP getter / setter方法

我想学习这个MVC OOP的thingie和我碰到一个奇怪的错误绊倒:

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

该代码我有:

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...

该FooBase类看起来是这样的:

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."); } }

所以,如果我理解正确的话,一个getter函数不能有参数呢? 为什么? 还是我做错了什么吗?

--------------解决方案-------------

任何与椭圆被视为一个方法。 神奇的__get__set方法只为东西看起来像属性的作用。

对于魔术的方法,请参阅__call()

分类:php 时间:2015-03-16 人气:309
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.463 (s). 11 q(s)