In PHP, is there a way to send a function's arguments straight to another function without having to specify them one by one? Is there a way to expand func_get_arg() so that the other function receives the individual arguments and not just a single array?
I'd like to send the arguments from foo() straight to bar() like so:
function foo($arg1, $arg2, $arg3)
{
$args = expand_args(func_get_arg());
bar($args);
}
yes.
function foo($arg1, $arg2, $arg3)
{
$args = func_get_arg();
call_user_func_array("bar",$args);
}
If you want to call it on a function belonging to an instance of an entirely seperate class, that can be done by passing the first arg to call_user_func_array as an array.
In this example, the function foo accepts whatever arguments, and passes them directly into $bar->baz->bob(), and returns the result.
public function foo(/* example arguments */)
{
return call_user_func_array
(
array($bar->baz, 'bob'),
func_get_args()
);
}
Related
I have the following code:
$array = array('foo', 'foo', 'bar', 'bis', 'ter')
Arrays::without($array, 'foo', 'bis') // Returns array('bar', 'ter')
As you can see, the second and third parameter to calling the Arrays::without function. you can pass as many parameters as you want, such as:
Arrays::without($array, 'foo', 'bis','athirdparam','afourthparam') // Returns array('bar', 'ter')
Im trying to encapsulate this into a static method inside a class i have:
public static function without($arr,$p)
{
return Arrays::without($arr,$p);
}
What i need to know is if there is a way i can pass unlimited arguments to this method without and use them inside my funciton call to Arrays::without
You can call:
Arrays::without($arr, ...$p);
Where $p is an array or write your method like that:
public static function without($arr,$p)
{
return Arrays::without($arr, ...$p);
}
But it's quite unnecessary.
Check splat operator: http://php.net/manual/en/migration56.new-features.php#migration56.new-features.splat
Either use func_get_args() inside the function, or public static function without(...$args) for the function signature.... it's all described here in the PHP Docs
In some MVC platforms, a controller method accepts an URL's contents as forward-slash separated list of elements, received as parameters, e.g.
site.com/controller/method/var1/var2
has associated controller:
class Controller
function method(var1, var2){
}
}
But how can I achieve this coding? I wish to start with an array and send a parameterized list to a function, i.e.
$args = array("one"=>"cheese","two"=>"eggs");
php_function("myfunction",$args);
Within myfunction, I would have
function myfunction($one, $two){
}
I know about func_get_args for accepting an unknown number of arguments. user_call_func is useful except
user_call_func("myfunction",$args);
...results in the first parameter containing an array of arguments, no difference to func_get_args called from within the function.
extract doesn't work either as I need to receive the array as a variable inside the function.
call_user_func_array takes a method description and an array of arguments, and returns the result of calling the function. I think it will take the arguments from the array in order though, rather than by name. You should assemble an array of the arguments in the correct order, and perhaps validate for missing arguments, before using this.
I've managed to create your desired with the following.
class BootStrap {
private $controller = 'NameOfIle.php';
private $method = 'function()';
private $params = array('sdfds' => 'dsfdsf', 'sdfdsfsdfdsfsd' => 'sdfdsfds');
public function __construct() {
call_user_func_array(array($this->controller, $this->method), $this->params);
}
}
I'm writing a Database wrapper class and need something like:
public function bind($types, $params, ...) {
$this->prep->bind_param($types, $params, ...);
}
How can I make the arguments dynamic, to have N-params?
I know of the function func_get_args() but doesn't help, I can fetch the arguments, but how to pass?
Off the top of my head, you could do it using call_user_func_array():
public function bind() {
$args=func_get_args();
$method=array($this->prep,'bind_param');
call_user_func_array($method,$args);
}
The function call_user_func_array should be what you need, something along the lines of the following:
public function bind () {
$args = func_get_args();
call_user_func_array(array($this->prep, "bind_param"), $args);
}
call_user_func and call_user_func_array can sometimes be a little slower than calling a method directly, unfortunately there isn't much you can do about this apart from hard code in the first few arguments.
Use an array, i advise you to use the classic way and keep some "core" arguments away from the array, than you can put optionnal ones in an array like so:
function function(Class $object, array $options = array()){
}
I have a function that takes a variable number of parameters, and I have to pass them by reference to another function.
Such as
function my_function($arg0, $arg1, $arg2, ...)
{
my_other_function(&$arg0, &$arg1, &$arg2, ...);
}
So that when I pass things by reference to my_function, my_other_function also gets them by reference.
Is there a way to do this?
I wonder why you need this. In general references are bad in PHP.
If you really want to do this the only proper way (ignoring call-time pass-by-ref hacks, which won't work with PHP 5.4 anymore anyways) is to use an array wrapping the parameters:
function myfunc(array $data) {
$data[0] += 42;
/* ... */
}
$var = 0;
myfunc(array(&$var /*, ... */));
echo $var; // prints 42
For passing to the other function you can then use call_user_func_array()
Technically you are not doing this:
function my_function($arg0, $arg1, $arg2, ...)
{
my_other_function(&$arg0, &$arg1, &$arg2, ...);
}
but this:
function my_function(&$arg0, &$arg1, &$arg2, ...)
{
my_other_function($arg0, $arg1, $arg2, ...);
}
However as #johannes already wrote, there is no good support for variable number of arguments that are references in PHP (and who could know better). func_get_argsDocs for example does not work with references.
Instead make use of the suggestion to pass all references via an array parameter. That works.
function my_function(array $args)
{
my_other_function($args);
}
This is an extension question of PHP pass in $this to function outside class
And I believe this is what I'm looking for but it's in python not php: Programmatically determining amount of parameters a function requires - Python
Let's say I have a function like this:
function client_func($cls, $arg){ }
and when I'm ready to call this function I might do something like this in pseudo code:
if function's first parameter equals '$cls', then call client_func(instanceof class, $arg)
else call client_func($arg)
So basically, is there a way to lookahead to a function and see what parameter values are required before calling the function?
I guess this would be like debug_backtrace(), but the other way around.
func_get_args() can only be called from within a function which doesn't help me here.
Any thoughts?
Use Reflection, especially ReflectionFunction in your case.
$fct = new ReflectionFunction('client_func');
echo $fct->getNumberOfRequiredParameters();
As far as I can see you will find getParameters() useful too
Only way is with reflection by going to http://us3.php.net/manual/en/book.reflection.php
class foo {
function bar ($arg1, $arg2) {
// ...
}
}
$method = new ReflectionMethod('foo', 'bar');
$num = $method->getNumberOfParameters();