1

I am looking for a way to return multiple parameters in a function. I am building a routing system for my MVC framework. And currently I am working on a way to get unlimited arguments in a function, based on an HTTP request;

This is the part I am stuck on. Look on the bottom of the code $controller->{$route->getMethod()}();

The controller calls a function, and now I need to get a solution to add values in de function. I can do $controller->{$route->getMethod()}($route->getArgs();

But then only the first argument will be set. How can I return more than one argument in that function?

public function dispatchRoute(Route $route)
{
    if(!file_exists($this->pathToController . '/' . ucfirst($route->getController()) . 'Controller.php'))
        throw new \Exception(
        sprintf('Cannot find %sController.php in directory %s', $route->getController(), $this->pathToController));

    require_once($this->pathToController . '/' . ucfirst($route->getController()) . 'Controller.php');

    $controller = $route->getController() . 'Controller';
    $controller = new $controller();

    if(!method_exists($controller, $route->getMethod()))
        throw new \Exception(sprintf('Method %s is not found in %sController', $route->getMethod(), $route->getController()));

    $dataHandler = new RequestDataHandler();
    $dataHandler->getRequestArguments();
    $controller->{$route->getMethod()}();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

3 Answers3

2

Return array.

function foo($bar1, $bar2, $bar3){
{
  return array($bar1, $bar2, $bar3);
}

Your example is not returning anything.. just printing.

Hardy
  • 5,590
  • 2
  • 18
  • 27
2

Either use

$data = $someclass->returnThreeValues();
foo($data[0], $data[1], $data[2];

or

list($p1, $p2, $p3) = $someClass->returnThreeValues();
foo($p1, $p2, $p3);

or (just to be complete, above options are preferred)

call_user_func_array('foo', $someClass->returnThreeValues());

And let

function returnThreeValues() {
    // Some code to calculate $a, $b, $c
    return array($a, $b, $c);
}
Peter van der Wal
  • 11,141
  • 2
  • 21
  • 29
0

Find it

call_user_func_array(array($controller, $route->getMethod()), array($dataHandler->getRequestArguments()));