Since PHP's call_user_method() and call_user_method_array() are marked deprecated I'm wondering what alternative is recommended?
One way would be to use call_user_func(), because by giving an array with an object and a method name as the first argument does the same like the deprecated functions. Since this function is not marked deprecated I assume the reason isn't the non-OOP-stylish usage of them?
The other way I can think of is using the Reflection API, which might be the most comfortable and future-oriented alternative. Nevertheless it's more code and I could image that it's slower than using the functions mentioned above.
What I'm interested in:
Is there a completely new technique for calling an object's methods by name?
Which is the fastest/best/official replacement?
What's the reason for deprecation?
As you said call_user_func can easily duplicate the behavior of this function. What's the problem?
The call_user_method page even lists it as the alternative:
<?php
call_user_func(array($obj, $method_name), $parameter /* , ... */);
call_user_func(array(&$obj, $method_name), $parameter /* , ... */); // PHP 4
?>
As far as to why this was deprecated, this posting explains it:
This is
because the call_user_method() and call_user_method_array() functions
can easily be duplicated by:
old way:
call_user_method($func, $obj, "method", "args", "go", "here");
new way:
call_user_func(array(&$obj, "method"), "method", "args", "go", "here");
Personally, I'd probably go with the variable variables suggestion posted by Chad.
You could do it using variable variables, this looks the cleanest to me. Instead of:
call_user_func(array($obj, $method_name), $parameter);
You do:
$obj->{$method_name}($parameter);
Do something like that :
I use something like that in my __construct() method.
$params = array('a','b','c'); // PUT YOUR PARAMS IN $params DYNAMICALLY
call_user_func_array(array($this, $this->_request), array($params));
1st argument : Obect instance,
2nd argument : method to call,
3rd argument : params
Before you can test if method or Class too, with :
method_exists()
class_exists()
Cheers
if you get following error:
Warning: call_user_func() expects parameter 1 to be a valid callback, second array member is not a valid method in C:\www\file.php on line X
and code like this:
call_user_func(array($this, $method));
use (string) declaration for $method name
call_user_func(array($this, (string)$method));
Related
I have found two solutinos on how to dynamically call a $function on an $instance with the $arguments provided as array.
Version with call_user_func_array
$result = call_user_func_array(array($instance, $function), $arguments);
Easier to read with three dots notation Argument unpacking
$result = $instance->$function)(...$arguments);
Now I only found pros for the second variant - according to some guy in the docs, it is even faster.
What are the differences? Are there any cons to not use the second variant over the first?
Note that they are actually doing different things:
Argument unpacking is just that, unpacking arguments to be used by a function.
call_user_func_array is calling a function and providing an array to be unpacked as arguments to that function.
In your case there won't be much of a difference as your code is doing essentially the same thing, but normally you will use call_user_func_array ONLY when you want to call a function via it, you won't use it just to unpack arguments (unless you are using PHP lower than 5.6).
TLDR:
While call_user_func_array does argument unpacking , it is not it's main idea, it's main idea is to call a function (and feed it arguments).
Also normally you shouldn't be calling functions like this and it signals that there is something terrible wrong with the code base, unless you have a very specific case.
While working with Laravel framework, more specific - Form macros, I stumbled upon a weird error.
At first, I thought it's something wrong with Laravel, but then I took everything out of context:
<?php
// placeholder function that takes variable as reference
$function = function(&$reference)
{
// append to variable
$reference = $reference . ':' . __METHOD__;
};
// test with straight call
$variable = 'something';
$function($variable);
echo $variable;
// test with call_user_func(), that gets called in Laravels case
$variable = 'something'; // reset
call_user_func($function, $variable);
echo $variable;
While the first call to $function executes properly, the second try with call_user_func(), produces (excerpt from Codepad):
Warning: Parameter 1 to {closure}() expected to be a reference, value given
PHP Warning: Parameter 1 to {closure}() expected to be a reference, value given
Fiddle: Codepad # Viper-7
While writing this, I thought about call_user_func_array(): fiddle here, but the same error is produced.
Have I got something wrong about references or is this a bug with PHP?
I would call this a bug with PHP, although it's technically a bug with call_user_func. The documentation does mention this, but perhaps not in a very enlightening way:
Note that the parameters for call_user_func() are not passed by
reference.
It would be perhaps clearer to say that the arguments to call_user_func() are not passed by reference (but note that technically it's not necessary to say anything at all; this information is also embedded in the function signature).
In any case, this means is that when call_user_func finally gets to invoking its target callable, the ZVAL (PHP engine internal data structure for all types of values) for the argument being passed is not marked as "being-a-reference"; the closure checks this at runtime and complains because its signature says that the argument must be a reference.
In PHP < 5.4.0 it is possible to work around this by using call-time pass by reference:
call_user_func($function, &$variable);
but this produces an E_DEPRECATED warning because call-time pass by reference is a deprecated feature, and will flat out cause a fatal error in PHP 5.4 because the feature has been removed completely.
Conclusion: there is no good way to use call_user_func in this manner.
This works:
call_user_func_array($function, array(&$variable));
I used this code
<?php
$myfunction = function &($arg=3)
{
$arg = $arg * 2;
return $arg;
};
echo $myfunction();
?>
Worked like a charm. :)
What happens if you do this?
call_user_func($function, &$variable);
I have a couple of libraries that use code similar to the following one.
$args = array_merge(array(&$target, $context), $args);
$result = call_user_func_array($callback, $args);
The code is different in both the cases, but the code I shown is what essentially is done. The $callback function uses the following signature:
function callback(&$target, $context);
Both the libraries document that, and third-party code (call it plug-in, or extension) adopts that function signature, which means none of the extensions defines the callback as, e.g., function my_extension_loader_callback($target, $context).
What confuses me are the following sentence in the documentation for call_user_func_array().
Before PHP 5.4, referenced variables in param_arr are passed to the function by reference, regardless of whether the function expects the respective parameter to be passed by reference. This form of call-time pass by reference does not emit a deprecation notice, but it is nonetheless deprecated, and has been removed in PHP 5.4. Furthermore, this does not apply to internal functions, for which the function signature is honored. Passing by value when the function expects a parameter by reference results in a warning and having call_user_func() return FALSE.
In particular, the highlighted sentence seems to suggest that is not done for functions define in PHP code.
Does using call_user_func_array() in this way work in PHP 5.4?
When using call_user_func_array, passing by value when a function expects a reference is considered an error, in newer versions of PHP.
This was valid PHP code before PHP 5.3.3:
//first param is pass by reference:
my_function(&$strName){
}
//passing by value, not by reference, is now incorrect if passing by reference is expected:
call_user_func_array("my_function", array($strSomething));
//correct usage
call_user_func_array("my_function", array(&$strSomething));
The above pass by value is no longer possible without a warning (my project is also set to throw exceptions on any kind of error (notice, warning, etc).) so I had to fix this.
Solution
I've hit this problem and this is how I solved it (I have a small RPC server, so there is no such thing as referenced values after deserializing params):
//generic utility function for this kind of situations
function &array_make_references(&$arrSomething)
{
$arrAllValuesReferencesToOriginalValues=array();
foreach($arrSomething as $mxKey=>&$mxValue)
$arrAllValuesReferencesToOriginalValues[$mxKey]=&$mxValue;
return $arrAllValuesReferencesToOriginalValues;
}
Although $strSomething is not passed by reference, array_make_references will make it a reference to itself:
call_user_func_array("my_function", array_make_references(array($strSomething)));
I think the PHP guys were thinking of helping people catch incorrectly called functions (a well concealed pitfall), which happens often when going through call_user_func_array.
If call_user_func_array() returns false you have a problem, otherwise everything should be fine.
Parameters aren't passed by reference by default anymore, but you do it explicitly.
The only trouble could be that your reference gets lost during array_merge(), haven't tested that.
I've found this same problem when upgrading to PHP5.4 when there were several sites using call_user_func_array with arguments passed by reference.
The workaround I've made is very simple and consists on replacing the call_user_func_array itself with the full function call using eval(). It's not the most elegant solution but it fits the purpose for me :)
Here's the old code:
call_user_func_array($target, &$arguments);
Which I replace with:
$my_arguments = '';
for ($i=0; $i<count($arguments); $i++) {
if ($i > 0) { $my_arguments.= ", "; }
$my_arguments.= "\$arguments[$i]";
}
$evalthis = " $target ( $my_arguments );";
eval($evalthis);
Hope this helps!
Considering that PHP has already deprecated a few functions and functionallitys, I would like to refactory my codes to fit it on php 5.3.
now, I have to eliminate all the 'Call-time pass-by-reference' . So, I have three questions:
1 - If I replace:
$myclass->myfunc(&$myvar);
by
$myvar = $myclass->myfunc($myvar);
will work ?
2 - what do I do if I have something like that?
$myclass->myfunc(&$myVar, &$ourvar);
3 - How about
$x = &new myclass();
Thanks for your time, any help will be very appreciated
First, are you familiar with what call-time pass by reverence is? Normal pass by reference is accomplished by the function declaring that certain arguments are pass by reference by prepending a & to the parameter in its declaration.
Call-time pass by reference means that the function is declared to take those arguments by value, and you are changing its behavior to pass-by-reference after the fact. Call-time pass by reference shouldn't really ever be necessary. Every function should have a specific purpose, and to correctly accomplish its purpose, it should either always take an argument by reference, or always take it by value. It is bad to make a function do something it was not designed to do.
Responding to your questions about $myclass->myfunc(&$myvar); and $myclass->myfunc(&$myVar, &$ourvar); I would say that, if you need to pass by reference to that function, then it should be declared as always pass by reference. i.e.
function myfunc(&$x, &$y) { ... }
Then to use it you just call it without the &
$myclass->myfunc($myVar, $ourvar);
$x = &new myclass(); is completely irrelevant. You are not passing anything. It is still valid syntax.
I'm calling a function with call_user_func_array :
call_user_func_array(array($this, 'myFunction'), array('param1', 'param2', 'param3'));
Everything is ok unless I don't know how many parameters the function needs.
If the function needs 4 parameters it sends me an error, I'd like to test if I can call the function (with an array of parameters).
is_callable() doesn't allow parameters check.
Edit : If the call fails I need to call another function, that's why I need a check.
Thanks!
You could use reflection to get the number of parameters:
$refl = new ReflectionMethod(get_class($this), 'myFunction');
$numParams = $refl->getNumberOfParameters();
or
$numParams = $refl->getNumberOfRequiredParameters();
See here for some more information
One way getting around this is to call the function always with a lot of arguments. PHP is designed in such a way that you can pass as many extraneous arguments as you want, and the excess ones are just ignored by the function definition.
See manual entry for func_get_args() to see an illustration about this.
Edit: As user crescentfresh pointed out, this doesn't work with built-in functions, only user defined functions. If you try to pass too many (or few) arguments into a built-in function, you'll get the following warning:
Warning: Wrong parameter count for strpos() in Command line code on line [...]