note 91503 modified in function.call-user-func-array by scottmac

From: Date: Mon, 27 Jul 2009 13:00:16 +0000
Subject: note 91503 modified in function.call-user-func-array by scottmac
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-158772@lists.php.net to get a copy of this message
Those having the passing by reference issue can use this simple hack. I´m really not sure WHY this works, but it does, and it does not make use of EVAL or other questionable functions. <?php function executeHook($name, $type='hooks'){ $args = func_get_args(); array_shift($args); array_shift($args); //Rather stupid Hack for the call_user_func_array(); $Args = array(); foreach($args as $k => &$arg){ $Args[$k] = &$arg; } //End Hack $hooks = &$this->$type; if(!isset($hooks[$name])) return false; $hook = $hooks[$name]; call_user_func_array($hook, $Args); } ?> All it´s doing is copying the args ($args) into a new array ($Args) by reference, which i would think would be identical to the original array in every way (that matters). Note the code here is an example of usage. The actual hack is denoted by comments. If someone knows a better alternative, by all means, i would love to see it. --was-- Those having the passing by reference issue can use this simple hack. I´m really not sure WHY this works, but it does, and it does not make use of EVAL or other questionable functions. <?php function executeHook($name, $type='hooks'){ $args = func_get_args(); array_shift($args); array_shift($args); //Rather stupid Hack for the call_user_func_array(); $Args = array(); foreach($args as $k => &$arg){ $Args[$k] = &$arg; //Why do we specify the key? Because PHP may change to disallow [] sometime in the future! } //End Hack $hooks = &$this->$type; if(!isset($hooks[$name])) return false; $hook = $hooks[$name]; call_user_func_array($hook, $Args); } ?> All it´s doing is copying the args ($args) into a new array ($Args) by reference, which i would think would be identical to the original array in every way (that matters). Note the code here is an example of usage. The actual hack is denoted by comments. If someone knows a better alternative, by all means, i would love to see it. http://php.net/manual/en/function.call-user-func-array.php

« previous php.notes (#158772) next »