zend-framework-php how to serialize/store closures (Closure)

Source: Internet
Author: User
Tags zend framework
In the latest Zend Framework 2.0, a large number of php5.3 closure features have been introduced. You can initialize a module by introducing a closure in the configuration file. (See: Http://packages.zendframework.com/doc ... )

However, the configuration file will have a cache, format conversion and other requirements (PHP = XML), at this time in the configuration file of the closure should be handled? Direct serialization of closed PHP is not supported.

Reply content:

In the latest Zend Framework 2.0, a large number of php5.3 closure features have been introduced. You can initialize a module by introducing a closure in the configuration file. (See: Http://packages.zendframework.com/doc ... )

However, the configuration file will have a cache, format conversion and other requirements (PHP = XML), at this time in the configuration file of the closure should be handled? Direct serialization of closed PHP is not supported.

Never used Zend, but I think ZF2 is just simple to copy the closure function code to a different format of the configuration file, the final is to re-write back to the PHP code format of the cache (can run directly), otherwise the runtime to parse the configuration file, the efficiency is poor.

You really need to serialize, you can use reflection (Reflection), and directly manipulate the code file to get contextual information:

/** * Create a reflection: */$reflection = new Reflectionfunction ($closure),/** * parameters can be directly obtained: */$params = $reflection->getparameters ();/** * Obtains the function body and the use variable of closure, such as: * Function ($arg 1, $arg 2, ...) use ($val 1, $val 2, ...) {*     //To get the code for this section! *} * Many ways, you can directly use regular, string lookup or Tokenizer, and so on. * For example, you can get the start and end lines of the function from reflection first: */$startLine = $reflection->getstartline (); $endLine = $reflection->getendline ();//Then use str* this, str* that function to clean up, the details do not write: $usedVars = use variables; $closureBody = function body;//...

At this point params,usedvars,closurebody and so on are just arrays and strings.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.