PHP 获取器/设置器

发布于 2024-11-05 14:12:29 字数 1390 浏览 7 评论 0原文

我正在尝试学习这个 MVC OOP 的东西,我偶然发现了一个奇怪的错误:

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 函数不能有参数?为什么?或者我在这里做错了什么?

I'm trying to learn this MVC OOP thingie and I stumbled across a weird error:

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

The code I have:

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 looks like this:

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, a getter function can not have arguments? Why? Or am I doing something wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再让梦枯萎 2024-11-12 14:12:29

任何带有省略号的内容都被视为方法。神奇的 __get__set 方法仅适用于看起来像属性的东西。

有关方法魔法,请参阅 __call ()

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

For method magic, see __call().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文