2013-05-14 68 views
6

有没有什么办法在PHP中序列化匿名函数?序列化php中的匿名函数

我发现这个http://www.htmlist.com/development/extending-php-5-3-closures-with-serialization-and-reflection/

protected function _fetchCode() 
{ 
    // Open file and seek to the first line of the closure 
    $file = new SplFileObject($this->reflection->getFileName()); 
    $file->seek($this->reflection->getStartLine()-1); 

    // Retrieve all of the lines that contain code for the closure 
    $code = ''; 
    while ($file->key() < $this->reflection->getEndLine()) 
    { 
     $code .= $file->current(); 
     $file->next(); 
    } 

    // Only keep the code defining that closure 
    $begin = strpos($code, 'function'); 
    $end = strrpos($code, '}'); 
    $code = substr($code, $begin, $end - $begin + 1); 

    return $code; 
} 

,但是这取决于内部实现封闭。

是否有任何未来的计划来实现闭包序列化?

+0

你想在某些时候将PHP函数传递给PHP?为什么? – BlitZ 2013-05-14 02:56:13

+0

假设我有一些用户界面组件库,我想给用户一定程度的自定义输出(通过匿名函数)。而且我希望能够在会话中保存对象的状态。 – Jarry 2013-05-14 04:01:48

回答