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'; // ...});