Linux and PHP web application support and development (Bromsgrove, UK)

Looking ahead to PHP 5.6

PHP 5.6 is almost here (we hope) – so we’ve had a look at some of the upcoming changes, and here are the ones of most interest to us. In a nutshell – variadics & splat, constant scalar expressions and ArrayOf type hinting ….

Main new features

  1. Large file upload support (>2gb) (almost hitting this issue when uploading large videos)
  2. Reduced memory usage in POST requests (any reduction in resource usage is always welcome!)
  3. Variadic functions (see below for an example)
  4. Argument Unpacking (aka ‘splat’, see below for an example)
  5. Constant Scalar Expressions (see below for an example)
  6. PHPDBG (PHP inbuilt debugger)
  7. ArrayOf as a type hint (see below for an example)

The code in the examples below was tested against a locally compiled binary from the PHP 5.6 Git branch on 20th Jan 2014 (compilation was an uneventful ./configure && make – so it should be easy enough for anyone to test out the new features).

Variadic functions

See also https://wiki.php.net/rfc/variadics

Aim – to allow functions to have variable arguments, and show this within the method signature.

PHP has always allowed extra (not defined) parameters to be passed to a function. To access these you’d have needed to call ‘func_get_args()’.

Now we can define a …$variable which will catch any extra parameters within the function signature.

So, for example, the following code :

function myFunction($parameter) 
{
    $otherArguments = array_slice(func_get_args(), 1);
}

Could be called like : myFunction("Hello", "World", false) (leaving $otherArguments to contain [“World”, false]).

With Variadic support, it could be written as :

function myFunction($parameter,  ...$otherArguments) 
{
    // No need for func_get_args()
}

Variadic parameters need to be a function’s last specified parameter.

If required, variadic parameters can be passed by reference ( &...$otherArguments ) and can have type hinting applied to them (i.e. every additional argument has to conform to the type hint).

Argument Unpacking / Splat Operator support

See also https://wiki.php.net/rfc/argument_unpacking

Aim – to make it easier to call a function using the contents of an array as it’s parameters.

So, if you have a array containing things you wish to use for parameters to a function call, you’d previously have had to do :

call_user_func_array('myFunction', $myParameters);

Now, assuming $myParameters contains [‘Hello’, ‘World’] the following will be equivalent in PHP 5.6 –

  1. call_user_func_array(‘myFunction’, $myParameters);
  2. myFunction( …$myParameters ); /* splat ! */
  3. myFunction(‘Hello’, ‘World’);

As with variadics, the unpacking can be performed by reference ( &…$myParameters ).

Just remember the array ($myParameters) cannot currently contain string keys – clearly someone’s hoping that PHP will one day support named parameters like Python (doing this will trigger :

Catchable fatal error: Cannot unpack array with string keys in ......./test.php on line 9

at present.

Constant Scalar Expressions

See also : https://wiki.php.net/rfc/const_scalar_exprs

Currently const, class property and function argument declarations have to be constant (unchanging) values. This RFC allows the declaration to be more expressive i.e. supporting the ternary operator, concatenation and all the normal arithmetic and bitwise operators.

For example, before PHP 5.6 we could have code like :

class Example 
{
    const CONSTANT = 4;
    const DATE = 2014;
    static $staticProperty = false;
    protected $property = [1,2,3];
}

With PHP 5.6, we can also have (I’ve shown as properties, but the same applies to consts or statics):

class Example 
{
    // in addition to the above ... 
    // concat. strings:
    protected $a = "Hello" . "world";
    protected $b = __DIR__ . '/something.php';
    // some maths:
    protected $c = 1 + 1;
    protected $d = self::CONSTANT + 2;
    protected $e = 1024 * 1024 * 2;
    // ternary operator ( x ? y : z ) :
    protected $f = 2014 < self::DATE ? true: false;
}

ArrayOf Type Hint

See also the RFC for ArrayOf

In a nutshell – this would allow for the enforcement of a specific object type within an array.

This seems to be quite a new RFC and hasn’t yet been merged into the 5.6 branch. Hopefully it will be included.

e.g.

Rather than :

function test(array $list) 
{
    foreach($list as $item) {
        if(! $item instanceof Something) {
            throw new RuntimeException('List item not a Something');
        }
    }
}

You could have :

function test(Something[] $list) 
{
    // everything in $a is an instanceof Something (And also not null).
}

So cutting down on some boilerplate code and so on – and improving the documentation of functions in the same way as the variadic support above.

, , ,

3 thoughts on “Looking ahead to PHP 5.6

Leave a Reply

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