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.
[code lang=”php”]
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";
[/code]
- 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.
[code lang=”php”]
<?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);
[/code]
Above output
[code]
1:0
1:1
1:2
[/code]
- 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.
[code lang=”php”]
<?
function add($a, $b, $c) {
return $a + $b + $c;
}
$operators = [2, 3];
echo add(1, …$operators);
[/code]
Above output
[code]6[/code]
- Exponentiation via **[code lang=”php”]
$a = 2 ** 3; // $a = 8, i.e. 2^3
$b = 2 ** 3 ** 2; // $b is 512 because its parsed as 2 ** (3 ** 2)[/code]
- Importing namespaced functions and constants
use function and use const. Earlier this operator was used with class only.
[code lang=”php”]
<?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();
}
[/code]
- 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