Closure-Guidance: php closures implement array formatting

Source: Internet
Author: User
I originally meant the following code {code ...} using closures {code ...} problem 1: php's closed-pack closure does not support returning parameter questions. 2. After using the use method, you still cannot format $ v, only the element use method {code...} can be deleted ...} I originally meant the following code:

                $eData=array(                'section'=>$v['section'],                'page'=>$v['page'],                //'status'=>$v['status'],                'type'=>$v['type'],            );                    unset($v['page']);                    //unset($v['status']);                    unset($v['id']);                    unset($v['type']);                    unset($v['section']);            $eData['data']=serialize($v);

Using closures

                $eData=array(                'section'=>$v['section'],                'page'=>$v['page'],                //'status'=>$v['status'],                'type'=>$v['type'],                'data'=>function(){                    unset($v['page']);                    //unset($v['status']);                    unset($v['id']);                    unset($v['type']);                    unset($v['section']);            return serialize($v);                    }            );

Problem 1: php's closed-pack closure does not support response parameters
Problem 2: After the use method is used, you cannot format $ v. You can only delete the elements.
Use Method

$eData=array( 'section'=>$v['section'],   'page'=>$v['page'], //'status'=>$v['status'], 'type'=>$v['type'], 'data'=>function() use($v){ unset($v['page']); //unset($v['status']); unset($v['id']); unset($v['type']); unset($v['section']);                                          return $v=serialize($v);}, );

Reply content:

I originally meant the following code:

                $eData=array(                'section'=>$v['section'],                'page'=>$v['page'],                //'status'=>$v['status'],                'type'=>$v['type'],            );                    unset($v['page']);                    //unset($v['status']);                    unset($v['id']);                    unset($v['type']);                    unset($v['section']);            $eData['data']=serialize($v);

Using closures

                $eData=array(                'section'=>$v['section'],                'page'=>$v['page'],                //'status'=>$v['status'],                'type'=>$v['type'],                'data'=>function(){                    unset($v['page']);                    //unset($v['status']);                    unset($v['id']);                    unset($v['type']);                    unset($v['section']);            return serialize($v);                    }            );

Problem 1: php's closed-pack closure does not support response parameters
Problem 2: After the use method is used, you cannot format $ v. You can only delete the elements.
Use Method

$eData=array( 'section'=>$v['section'],   'page'=>$v['page'], //'status'=>$v['status'], 'type'=>$v['type'], 'data'=>function() use($v){ unset($v['page']); //unset($v['status']); unset($v['id']); unset($v['type']); unset($v['section']);                                          return $v=serialize($v);}, );

Is this effect required?

$eData = array(    'section' => $v['section'],    'page'    => $v['page'],    'type'    => $v['type'],    'data'    => call_user_func(        function() use (&$v)        {            unset($v['page']);            unset($v['id']);            unset($v['type']);            unset($v['section']);            return serialize($v);        }    ),);

After a rough look, you should want 'data' to be serialize ($ v? That is to say, remove the 'section ', 'page', and 'type' and then serialize the string. 'Data' => function () assigns an anonymous function to data, instead of the return value to data.
The second question is too silly.

'data'=>function() use($v){ unset($v['page']); //unset($v['status']); unset($v['id']); unset($v['type']); unset($v['section']);                                          return $v=serialize($v)

The use syntax is used to re-copy the transfer, not to reference, to convert $ v into a serialized value, it should be a referenceuse(&$v)

What people say on the second floor is right. Why don't they accept it?

In this way, you pass an anonymous function to $ eData ['data'] without returning the function results to it.

I don't know what the closure is like.
Just answer the question according to your understanding.

Write as follows:

class A {    var $eData = Array();    function formatArrayWithHookFn( $hookFn ) {        $hookFn( $this->$eData );    }}$a = new A();$a->formatArrayWithHookFn(function( $arrayToFormat ) {    $arrayToFormat['foo'] = 'bar';    // ...});

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.