2013-08-04 38 views
0

$this不支持php5.3.x的类中的匿名函数,我该如何解决这个问题,以便我可以将$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()); 

结果,

init Fatal error: Using $this when not in object context in /home/content/95/10799595/html/bin/test/anonymous_4.php on line 41 --> if(!$options) $options = $this->options;

我应该得到这个在我的本地主机是在php5.4,

init array (size=1) 0 => string 'hello world!' (length=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); 

    } 

仍然得到一个错误。

Fatal error: Cannot access private property myclass::$options in /home/content/95/10799595/html/bin/test/anonymous_4.php on line 43

回答

2

只是

$foobar = $this; 

$anonymous = function() use($foobar) { 

}; 

作品。

这是愚蠢的,但这是PHP ... :)

+0

谢谢。我尝试过,但仍然出现错误。请参阅我上面的编辑。谢谢。 – laukok

+0

你需要告诉哪一行是43。 FWIW,我真的不明白你想要取得什么成就,这似乎太复杂了。可能明智的看看,简单的匿名函数,而不是那种东西会更好地工作:) – Smar

+0

只需制作一个公共getOptions方法。你不能通过'$ self'直接访问任何私人内容。 – danronmoon

相关问题