D
D
dk-web2015-10-01 10:51:12
PHP
dk-web, 2015-10-01 10:51:12

How to get (use) an array of parameters call_user_func_array?

Anyway, back to one question..
In the index.php entry point, I run the application class...
$app=new App();
in the App class in the constructor

public function __construct (){
...
call_user_func_array([$this->controller,$this->method],$this->params);
}

address:
localhost/myproj/map(controller)/lal(method)/foo(parameter_1)/bar(parameter_2)
In the controller, example:
public function lal($param1=NULL,$param2) {
    echo $param1; //foo
    echo $param2; //bar
  }

Is it possible to get all the parameters in an array? Or is the only way to pass it to the controller's constructor when instantiating it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2015-10-01
@dk-web

php.net/manual/en/function.func-get-args.php

S
shagguboy, 2015-10-01
@shagguboy

https://wiki.php.net/rfc/argument_unpacking
n PHP 5.6 and later, argument lists may include the ... token to denote that the function
accepts a variable number of arguments. The arguments will be passed into the given variable as an array; for example:
Example #13 Using ... to access variable arguments
<?php
function sum(...$numbers) {
$acc = 0;
foreach ($numbers as $n) {
$acc += $n;
}
return $acc;
}
echo sum(1, 2, 3, 4);
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question