php - Passing a reference using call_user_func_array with variable arguments -
i have following code:
<?php class testme { public static function foo(&$ref) { $ref = 1; } } call_user_func_array(array('testme', 'foo'), array(&$test)); var_dump($test);
and correctly displays "1". want same, using "invoker" method, following:
<?php class testme { public static function foo(&$ref) { $ref = 1; } } class invoker { public static function invoke($func_name) { $args = func_get_args(); call_user_func_array(array('testme', $func_name), array_slice($args,1)); } } $test = 2; invoker::invoke('foo', $test); var_dump($test);
this throws strict standards error (php 5.5) , displays "2"
the question is, there way pass arguments reference testme::foo
, when using func_get_args()
? (workarounds welcome)
there no way reference out of func_get_args()
because returns array copy of values passed in. see php reference.
additionally, since runtime pass reference no longer supported, must denote reference in each method/function signature. here example should work around overall issue of having invoker pass reference, there no work around func_get_args()
.
<?php class testme { public static function foo(&$ref) { $ref = 1; } } class invoker { public static function invoke($func_name, &$args){ call_user_func_array(array('testme', $func_name), $args); } } $test = 10; $args[] = &$test; invoker::invoke('foo', $args); var_dump($test);
if know want invoke reference, can work , perhaps have 2 invokers, 1 invoker::invokebyref
normal invoker::invoke
standard invoking copy.
Comments
Post a Comment