|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-09-12 11:00 UTC] maarten at ba dot be
Description:
------------
i'm writing code that invokes "hooks" with byref parameters.
but the generalized hook functions lose the byref information due to func_get_args being a copy (but not exact), thus call_user_func_array() (which needs to have references in it's array) can't work.
see shortened example below:
Test script:
---------------
$foo = "foo";
$bar = "bar";
function func_a(&$a, $b) {
$a = $b;
};
function c() {
$args = func_get_args();
call_user_func_array("func_a", $args);
};
c(&$foo, $bar);
var_dump($foo);
Expected result:
----------------
i expected for $foo to have 'bar'
Actual result:
--------------
$foo is still 'foo', and a warning is generated
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 06 18:00:01 2025 UTC |
actually, i found a second valid use case for this oversight: function array_set(&$a, $indexes, &$value) { $res = FALSE; if (count($indexes) == 0) { $a =& $value; return $res; } $i = array_shift($indexes); if (!isset($a[$i])) { $a[$i] = array(); $res = TRUE; } return array_set($a[$i], $indexes, $value) || $res; } function set_config() { global $config; $args = func_get_args(); $value = array_pop($args); return array_set($config, $args, $value); } global $config; $config = array( 'user' => array( 'name' => '', ) ); set_config('user', 'name', 'FName LName'); var_dump($config); the way i see it, there are 2 possibilities, really: A) func_get_args should keep references for references ==> one could argue that byref parameters in the call is a stupid idea B) functions that are declared with byref parameters should be greedy and force the variables to be a reference, if they must be (and where they come from), and that is what should force one argument to be byref in a func_get_args, even if it wasn't like this. ==> this way, you can safely have no use for byref parameters anymore.