PHP 5.6: What’s new features

PHP 5.6 has been released and is one of very important release. It brings the new features and many improvements and few backward incompatibility also.

Among the main features:

  • Constant scalar expressions

It is now possible to provide a scalar expression involving numeric and string literals and/or constants in contexts where PHP previously expected a static value, such as constant and property declarations and default function arguments.


const ONE = 1;

// Scalar Expression in constant
const TWO = ONE * 2;
 
class foo {
    // Scalar expression in class property   
    const THREE = TWO + 1;
     
    // Scalar expression in class methods
    public test($a = ONE + self::THREE) {
        return $a;
    }
}
 
echo (new foo)->test()."\n";

 
  • Variadic Functions via “…”

Earlier we were using func_get_args() to get all the arguments available in a function call(where-ever argument length is variable), but with PHP 5.6, this can be removed as we can easily use … operator. We can also type-hint the parameter.

 
<?php
function f($req, ...$params) {
    // $params is an array containing the remaining arguments.
    echo $req . ' : ' .  count($params)
}

f(1);
f(1, 2);
f(1, 2, 3);

Above output

 
1:0
1:1
1:2

  • Argument unpacking via …

Arrays and Traversable objects can be unpacked into argument lists when calling functions by using the … operator. This is also known as the splat operator in other languages, including Ruby.

<?
function add($a, $b, $c) {
    return $a + $b + $c;
}

$operators = [2, 3];
echo add(1, ...$operators);

Above output

6
  • Exponentiation via **
    $a = 2 ** 3; // $a = 8, i.e. 2^3
    $b = 2 ** 3 ** 2; // $b is 512 because its parsed as 2 ** (3 ** 2)
    
    
  • Importing namespaced functions and constants

use function and use const. Earlier this operator was used with class only.

<?php
namespace Name\Space {
    const FOO = 42;
    function f() { echo __FUNCTION__."\n"; }
}

namespace {
    use const Name\Space\FOO;
    use function Name\Space\f;

    echo FOO."\n";
    f();
}

  • Large file uploads

Uploads of over 2GB are now accepted

  • php://input is reusable

This means you can read it several times now, and it always reads from the same data, which has resulted in a significant memory usage reduction.

  • Default character encoding

The default character encoding has been set to UTF-8

  • A new command line debugger called phpdbg

Check out what’s new in php 5.6

Backward Incompatible Changes:

  • Array keys won’t be overwritten when defining an array as a property of a class via an array literal.
  • json_decode() is more strict in JSON syntax parsing.
  • Stream wrappers now verify peer certificates and host names by default when using SSL/TLS.
  • GMP resources are now objects.
  • Mcrypt functions now require valid keys and IVs.

Check out changelog List of changes into function in PHP 5.6

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s