abstract class theBase { function theFunc($min,$max) { $func=function($value) { $increase=function(&$var) { $var++; return ($var+2); }; return ($increase($value)); }; return (array_map($func,range($min,$max))); } } class theChild extends theBase { function doMoreWork(&$var) { $var++; return($var+3); } } $theClass=new theChild; $theArray=call_user_func_array(array($theClass,"theFunc"),array(3,5,7)); array_walk($theArray,array($theClass,"doMoreWork")); print_r($theArray); PHP: the output is :7,8,9 Can someone help why 7,8,9 and not 11,12,13 .In doMoreWork function ($var+3) is showing no effect why? Can someone help to understand this behavior.
Well first when I look at $increase, &$var has no purpose as array_map() is simply calling $func for every value in the range($min,$max) = range(3,5). With each new call, $var is in it's own scope (has a different memory address) so a parameter of $var would not differ from &$var in this case. For theChild::doMoreWork(), return $var+3 has no affect as array_walk()'s first parameter is &array(). Thus the values of &array will be changed according to $var in theChild::doMoreWork(), the first (and only) parameter. What you have is really the same as: abstract class theBase { function theFunc($min,$max) { $func=function($value) { $increase=function($var) { return ($var+3); }; return ($increase($value)); }; return (array_map($func,range($min,$max))); } } class theChild extends theBase { function doMoreWork(&$var) { $var++; } } $theClass=new theChild; $theArray=call_user_func_array(array($theClass,"theFunc"),array(3,5)); array_walk($theArray,array($theClass,"doMoreWork")); print_r($theArray); PHP: