php - Does the following call to call_user_func_array() work? -


i have couple of libraries use code similar following one.

$args = array_merge(array(&$target, $context), $args); $result = call_user_func_array($callback, $args); 

the code different in both cases, code shown done. $callback function uses following signature:

function callback(&$target, $context); 

both libraries document that, , third-party code (call plug-in, or extension) adopts function signature, means none of extensions defines callback as, e.g., function my_extension_loader_callback($target, $context).

what confuses me following sentence in documentation call_user_func_array().

before php 5.4, referenced variables in param_arr passed function reference, regardless of whether function expects respective parameter passed reference. form of call-time pass reference not emit deprecation notice, nonetheless deprecated, , has been removed in php 5.4. furthermore, not apply internal functions, function signature honored. passing value when function expects parameter reference results in warning , having call_user_func() return false.

in particular, highlighted sentence seems suggest not done functions define in php code.

does using call_user_func_array() in way work in php 5.4?

when using call_user_func_array, passing value when function expects reference considered error, in newer versions of php.

this valid php code before php 5.3.3:

//first param pass reference: my_function(&$strname){ }  //passing value, not reference, incorrect if passing reference expected: call_user_func_array("my_function", array($strsomething));  //correct usage call_user_func_array("my_function", array(&$strsomething)); 

the above pass value no longer possible without warning (my project set throw exceptions on kind of error (notice, warning, etc).) had fix this.

solution i've hit problem , how solved (i have small rpc server, there no such thing referenced values after deserializing params):

//generic utility function kind of situations function &array_make_references(&$arrsomething) {      $arrallvaluesreferencestooriginalvalues=array();     foreach($arrsomething $mxkey=>&$mxvalue)         $arrallvaluesreferencestooriginalvalues[$mxkey]=&$mxvalue;     return $arrallvaluesreferencestooriginalvalues; } 

although $strsomething not passed reference, array_make_references make reference itself:

call_user_func_array("my_function", array_make_references(array($strsomething))); 

i think php guys thinking of helping people catch incorrectly called functions (a concealed pitfall), happens when going through call_user_func_array.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -