Argument unpacking in PHP5.6

PHP 5.6 comes with some new features. This post is to preview the Argument unpacking.

The Argument unpacking introduced a new operator  It can be used in a function parameter to indicate to that function that the items of an array or a Traversable must be used as the parameters of that function.

Here is an example of how this new feature is used with the MVC pattern: (For simplicity I’m going to use one controller class and hard code it in the router class.

<?php

class Router {

     public function start($action, $params = [])
     {
         // Contruct the method name
         $method = $action . 'Action';

         // Execute the controller method action with the parameters
         $controller = new IndexController;
         return $controller->$method(...$params);
     }
}

class IndexController {

    /**
     * Home page action
     *
     * @param string $name
     * @return string
     */
    public function homeAction($name)
    {
        return "Hello " . $name . "\n";
    }

    /**
     * Products list action
     *
     * @param array $products
     * @param int $countProducts
     * @param string $currentCategory
     * @return string
     */
    public function productsAction($products, $countProducts, $currentCategory)
    {
        $output = "Total Number of products: " . $countProducts . "\n";
        $output .= "In Category: " . $currentCategory . "\n";

        foreach ($products as $product) {
            $output .= "\t- " . $product . "\n";
        }

        return $output;
    }
}

// Usage...
$router = new Router;

// Display home action
echo $router->start('home', ['Mohamed']);

echo "\n";

// Display products list action
$products = [
    'Product 1',
    'Product 2',
    'Product 3',
    'Product 4',
];
$params = [
    $products,
    count($products),
    'Category 1'
];
echo $router->start('products', $params);

The argument unpacking is in the router method Router::start. Previously you would have had to rely on the function call_user_func_array(), but now we can use the argument unpacking This makes the code cleaner and more easier to read and understand.

<?php

//... class code
     public function start($action, $params = [])
     {
         // Contruct the method name
         $method = $action . 'Action';

         // Execute the controller method action with the parameters
         $controller = new IndexController;
         return call_user_func_array(array($controller, $method), $params);
     }
<?php

//... class code
     public function start($action, $params = [])
     {
         // Contruct the method name
         $method = $action . 'Action';

         // Execute the controller method action with the parameters
         $controller = new IndexController;
         return $controller->$method(...$params);
     }

In the end all I have to say is: “Thank you PHP team for this improvement and please keep more of this good work coming!”

Posted in PHP and tagged , .
Loading Facebook Comments ...

Leave a Reply

Your email address will not be published. Required fields are marked *