【问题标题】:PHP anonymous functions: how to pass $this and write a fallback?PHP 匿名函数:如何传递 $this 并编写回退?
【发布时间】:2013-08-04 16:09:09
【问题描述】:

php5.3.x 的类中的匿名函数不支持$this,我该如何解决这个问题,以便将$this info/ 数据传递给匿名函数?如何编写回退到 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 上的本地主机中得到这个,

初始化数组 (size=1) 0 => 字符串 'hello world!' (长度=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 in /home/content/95/10799595/html/bin/test/anonymous_4.php 在第 43 行

【问题讨论】:

    标签: php oop anonymous-function


    【解决方案1】:

    只是

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

    有效。

    这很傻,但那是 php... :)

    【讨论】:

    • 谢谢。我试过了,但仍然出现错误。请参阅我上面的编辑。谢谢。
    • 你需要知道第 43 行是哪一行。 FWIW,我不太了解您首先要实现的目标,这似乎太复杂了。看看简单的匿名函数是否比那种东西更好用可能是明智的:)
    • 只需要公开一个 getOptions 方法。您无法通过$self 直接访问任何私人内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2019-11-23
    • 2011-05-11
    • 2011-01-29
    • 1970-01-01
    相关资源
    最近更新 更多