在php中序列化匿名函数

时间:2013-05-14 02:37:37

标签: php serialization anonymous-function

有没有办法在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;
}

但这取决于闭包的内部实现。

有未来计划实施封闭序列化吗?

1 个答案:

答案 0 :(得分:3)

看看我在这里关于PHP Super Closure的回复:

Exception: Serialization of 'Closure' is not allowed

我希望它有所帮助。