Serializing anonymous functions in php

為{幸葍}努か 提交于 2019-12-09 16:43:22

问题


is there any way to serialize an anonymous function in php?

i have found this 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;
}

but it depends on the internal implementation of closure.

are there any future plans to implement closure serialization?


回答1:


Take a look to my response here about PHP Super Closure :

Exception: Serialization of 'Closure' is not allowed

I hope it helps.



来源:https://stackoverflow.com/questions/16534245/serializing-anonymous-functions-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!