What’s new in PHP 5.6
-
Wiki: PHP 5.6
-
php.net: New features of PHP 5.6.
- habrahabr.ru: Other PHP 5.6 features not related to syntax.
const PLUS = 1 + 2; — scalar expressions in constants/properties/function arguments
Now it is possible to specify primitive PHP expressions in constant values.
More precisely, the feature concerns not only constants, but everything where PHP previously expected a static value. Now instead of static value you can specify an expression of numbers/strings/constants. If more precise, the PHP expression can be specified: in constants/class properties and in the default value of a function argument.
const ONE = 1;
const TWO = ONE * 2;
class C {
const THREE = TWO + 1;
const ONE_THIRD = ONE / self::THREE;
const SENTENCE = 'The value of THREE is '. self::THREE;
public function f( $a = ONE + self::THREE ){
return $a;
}
}
echo (new C)->f() .' - '. C::SENTENCE; //> 4 - The value of THREE is 3
const ARR = ['a', 'b']; — a constant can hold an array
It became possible to hold arrays in a constant:
const ARR = ['a', 'b']; echo ARR[0]; //> a
func( ...$args ) or func( ...[2, 3] ) — packing/unpacking function parameters
Wiki: Argument Unpacking
The operator ... is also called the “Splat Operator”, the “Scatter operator” or the “Spread operator”.
When we did not know in advance how many parameters a function could receive, we had to process the passed parameters inside the function using special functions: func_num_args(), func_get_arg(), func_get_args().
Packing transmitted parameters into a single variable when declaring a function
Now they are not needed and we can receive all parameters in a single variable; to do this, place the operator ... before that variable:
function sum( ...$numbers ){
$plus = 0;
foreach( $numbers as $n ){
$plus += $n;
}
return $plus;
}
echo sum( 1, 2, 3 ); //> 6
Another example:
function func( ...$numbers ){
print_r( $numbers, 1 );
}
func( 1, 2, 3 );
/*
We get:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
*/
Unpacking transmitted parameters when calling a function
Now using the splat operator ..., you can specify function parameters directly from array values:
function plus( $a, $b, $c ){
return $a + $b + $c;
}
$array = [ 2, 3 ];
echo plus( 1, ...$array ); //> 6
// or like this
echo plus( 1, ...[ 2, 3 ] ); //> 6
Replacing the call_user_func_array() function
Now call_user_func_array( $callback, $param_arr ), which is usually not the fastest, can be replaced like this:
$params = [ 1, 2, 3 ]; $callback( ...$params );
Unpacking into an array
Associative arrays cannot be unpacked.
// $arr1 = [ 'key' => 1 ] would cause a Fatal error $arr1 = [ 'foo', 100 ]; $arr2 = [ 'val', 200 ]; $arr = [ 1,89, 'str', ...$arr1, 22 ,...$arr2, 456, 52 ]; print_r( $arr ); /* Array ( [0] => 1 [1] => 89 [2] => str [3] => foo [4] => 100 [5] => 22 [6] => val [7] => 200 [8] => 456 [9] => 52 ) */
** — exponentiation operator
Until PHP 5.6, to raise a number to a power you had to use the pow(2,2); function, and now there is the ** operator:
// example 1 echo $a = 2 ** 2; //> 4 // example 2 $a = 2; echo $a **= 2; //> 4 // example 3 echo $a = 2 ** 3 ** 2; //> 512 = 2^9
use function and use const — importing functions and constants into the namespace
Now it is possible using the use keyword to import functions or constants from another namespace into ours:
namespace our\space {
const FOO = 42;
function func() { echo __FUNCTION__; }
}
namespace my\space {
use const our\space\FOO;
use function our\space\func;
echo FOO .' - '. func(); //> 42 - our\space\func
}—