How to serialize/store closures in zend-framework-php)

Source: Internet
Author: User
In the latest ZendFramework2.0, the closure feature of php5.3 has been widely used. You can introduce a closure in the configuration file to initialize the module. (See packages. zendframework. comdoc ...) however, the configuration file will have cache and format conversion requirements (p... in the latest Zend Framework 2.0, the closure feature of php5.3 has been widely used. You can introduce a closure in the configuration file to initialize the module. (See: http://packages.zendframework.com/doc ...)

However, the configuration file will have cache, format conversion, and other requirements (php => xml). What should I do with the closure in the configuration file? Php does not support directly serializing closures.

Reply content:

In the latest Zend Framework 2.0, the closure feature of php5.3 has been widely used. You can introduce a closure in the configuration file to initialize the module. (See: http://packages.zendframework.com/doc ...)

However, the configuration file will have cache, format conversion, and other requirements (php => xml). What should I do with the closure in the configuration file? Php does not support directly serializing closures.

Zend has never been used, but I think ZF2 simply copies the code of the closure function to the configuration files in different formats, in the end, it must be written back to the PHP code format cache (which can be run directly). Otherwise, the configuration file will be parsed during the runtime, which is too inefficient.

Serialization is required. You can use Reflection and directly operate the code file to obtain the context information:

/*** Create a reflection: */$ reflection = new ReflectionFunction ($ closure);/*** the parameter can be directly obtained: */$ params = $ reflection-> getParameters ();/*** obtain the Closure function body and use variables, for example, * function ($ arg1, $ arg2 ,...) use ($ val1, $ val2 ,...) {* // obtain the code for this part! *} * There are many methods. You can use regular expressions, string searches, Tokenizer, and so on. * For example, you can first obtain the start and end rows of the function from reflection: */$ startLine = $ reflection-> getStartLine (); $ endLine = $ reflection-> getEndLine (); // use the str * And str * functions to clear the data. The details are not written: $ usedVars = use variables; $ closureBody = function body ;//...

Now 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.