Variadic functions in PHP5.6

PHP 5.6 comes with some new features. This post is to preview the Variadic functions

This new feature looks as follows:

<?php

function addNumbers(...$params) {
    // $params is an array containing the function parameters
}

// Usage
echo addNumbers(1, 3, 5);

We have a new operator and it is to be a replacement for the usage of func_get_args() to access the function parameters.

In my opinion relying on the func_get_args() to access the function parameters is not good programming practice.

<?php

function addNumbers() {
    $params = func_get_args();
    return array_sum($params);
}

It is difficult to know how to use the above function. What are the parameters and their types? What is expected from the function and the expected return? The function name indicates that it will add numbers, but where are the parameters? How can the function be used?

Why use the Variadic functions:

– Cleaner function definition

The definition of the function indicates that it uses Variadic functions. The function accepts n number of parameters that are going to be merged into an array.

– Type Hinting

Type hinting can be used on the parameters to make sure that they are of a specific type. While, it is not possible to add type hinting when using func_get_args().

<?php

function addProduct(Product ...$product) {
    // $product will always be an array containing items of a type Product
}
– Less unrelated code in the function body

The body of the function does not have to include code to deal with the n number of parameters. It now includes the logic of the function only.

<?php

function addNumbers() {
    // Get the parameters
    $params = func_get_args(); // --> no need for this line of code and the one above it!
    return array_sum($params);
}
– Reduce the lines of code

In the example below, we used the same function to add product(s), with type hinting and the ability to add a product as a single variable or an array of products.

<?php

class Product
{
    protected $name;

    public function __construct($name)
    {
        $this->name = $name;
    }
}

class Category
{

    protected $products = [];

    public function add(Product ...$product)
    {
        $this->products += $product;
        return $this;
    }
}

// Usage...

$category = new Category;

$product1 = new Product('Product 1');
$product2 = new Product('Product 2');

// Add 2 products
$category->add($product1, $product2);

// Add 1 product
$category->add($product1);

Without the new feature the above Category method might look like:

</php

//.... class code

    public function add(array $products)
    {
        $this->products += $products;
        return $this;
    }

However here we expect the array to contain items of a type Product. Otherwise, we would have to add validation code before adding the products. Also, if we are to add one product, we would have to create an array containing the product and then call the method!

I think calling the method this way:

<?php

$category->add($product1, $product2);

Is much cleaner than calling the function with an array

<?php

$category->add([$product1, $product2]);

or having to create 2 methods, one for adding multiple products and the other one for adding a single product.

What is missing?

– An exception error

Using the above Product/Category example, if you execute the following code, php will throw a fatal error. “PHP Catchable fatal error: Argument 3 passed to Category::add() must be an instance of Product, string given,…”

It would be nice if this was an exception that can be handled, instead of a fatal error.

<?php

$category->add($product1, $product2, 'Product 3');
– Not allowing empty parameters with type hinting

In the above Product/Category it is possible to execute the following code. The code does nothing, but it is valid.

It would be nice if php would throw exception to indicate that the function is expecting at least one parameter of an instance of Product!

<?php

$category->add();

In the end all I have to say, Thank you PHP team for this improvement and please keep the good work coming fast!

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

Leave a Reply

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