相关疑难解决方法(0)

如何在PHP中实现__isset()魔术方法?

我正在尝试使用类似函数empty()isset()使用方法返回的数据.

到目前为止我所拥有的:

abstract class FooBase{

  public function __isset($name){
    $getter = 'get'.ucfirst($name);
    if(method_exists($this, $getter))
      return isset($this->$getter()); // not working :(
      // Fatal error: Can't use method return value in write context 
  }

  public function __get($name){
    $getter = 'get'.ucfirst($name);
    if(method_exists($this, $getter))
      return $this->$getter();
  }

  public function __set($name, $value){
    $setter = 'set'.ucfirst($name);
    if(method_exists($this, $setter))
      return $this->$setter($value);
  }

  public function __call($name, $arguments){
    $caller = 'call'.ucfirst($name);
    if(method_exists($this, $caller)) return $this->$caller($arguments);   
  }

}
Run Code Online (Sandbox Code Playgroud)

用法:

class Foo extends FooBase{
  private $my_stuff;

  public function …
Run Code Online (Sandbox Code Playgroud)

php class isset magic-methods

11
推荐指数
1
解决办法
7266
查看次数

标签 统计

class ×1

isset ×1

magic-methods ×1

php ×1