PHP匿名函数:如何传递$this并编写回退


PHP anonymous functions: how to pass $this and write a fallback?

php5.3.x类中的匿名函数不支持$this,我该如何处理这一问题,以便将$this信息/数据传递给匿名函数?如何编写php5.3.x的回退?

class anonymous {
    protected $methods = array();
    public function __construct(array $options)
    {
        $this->methods = $options;
    }
    public function __call($name, $arguments)
    {
        $callable = null;
        if (array_key_exists($name, $this->methods))
            $callable = $this->methods[$name];
        elseif(isset($this->$name))
            $callable = $this->$name;
        if (!is_callable($callable))
            throw new BadMethodCallException("Method {$name} does not exists");
        return call_user_func_array($callable, $arguments);
    }
}
class myclass {
    private $options = array();
    public function __construct($options = array()){
        $this->options = $options;
    }
    public function hello($data = null, $options = array()){
        $methods = new anonymous(array(
            "init" => function($options) { 
                echo "init";
                if(!$options) $options = $this->options;
                return $options; 
            }, 
            "run" => function($options) { 
                echo "run";
                return $options; 
            } 
        ));
        $default = array(
            "method" => "init",
            "options" => array()
        );
        if($data === null) {
            $method = "init";
        } else if (is_string($data)) { 
            $method = $data;
        } else if(is_array($data)) {
            $method = "init";
            $options = $data;
        }
        // Return the requested method.
        return $methods->$method($options);
    }
}

因此,

$myclass = new myclass(array("hello world!"));
var_dump($myclass->hello());

结果,

初始化致命错误:在第41行的/home/content/95/10799595/html/bin/test/anonymous_4.php的对象上下文中不使用$this-->if(!$options)$options=$this->options;

我应该在php5.4上的localhost中得到这个,

init数组(size=1)0=>string"你好,世界!"(长度=12)

有什么解决方案和建议吗?

编辑:

public function hello($data = null, $options = array()){
        $self = $this;
        $methods = new anonymous(array(
            "init" => function($options) use($self){ 
                echo "init";
                if(!$options) $options = $self->options;
                return $options; 
            }, 
            "run" => function($options) { 
                echo "run";
                return $options; 
            } 
        ));
        $default = array(
            "method" => "init",
            "options" => array()
        );
        if($data === null) {
            $method = "init";
        } else if (is_string($data)) { 
            $method = $data;
        } else if(is_array($data)) {
            $method = "init";
            $options = $data;
        }
        // Return the requested method.
        return $methods->$method($options);
    }

仍然会出现错误。

致命错误:无法访问中的私有属性myclass::$options/home/content/95/10799595/html/bin/test/anymous_4.php,第43行

$foobar = $this;
$anonymous = function() use($foobar) {
};

工作。

这很傻,但那是php…:)