PHP5.6 is, at the date of writing this, in the first beta phase. So the good question is: What new and cool features can we expect in the shiny new PHP-Version?
- Exponential operator
- importing namespaced functions
- constant scalar expressions
- variadic functions
- argument unpacking
- phpdbg
- Streams for POST-data
- Default Character-Encoding improvements
- TLS improvements
- More “under-the-hood”-Improvements
For a full list of changes have a look at the RFC-Part of the php.net wiki
Exponential operator
Changes the way to get exponential numbers like 2 to the power of 8 (which should be 256)
Old style:
$number = pow(2, 8);
New Style:
$number = 2 ** 8;
// alternate
$number = 2;
$number **= 8;
Importing Namespaced functions
Namespaces shouldn’t be something new by now. Namespaces have been introduced in PHP 5.3. BUt until now you could not use functions from a different namespace. Have a look at the following code:
namespace foo {
function bar {
return 'foo.bar';
}
}
namespace bar {
use function foo.bar AS foobar;
var_Dump(foobar());
}
Constant scalar expressions
In places where until PHP5.5 you can only make a simple declaration you can now also use static scalar expressions.
Some code examples Old School:
class foo {
protected $a = 1;
const B = 2;
function __construct()
{
$this->a += 2;
}
function foo($a = 1)
{
// do something
}
}
New Way:
class foo {
protected $a = 1 + 2;
const B = $this->a - 1
function foo($a = ($this->a)?2:200)
{
// do something
}
}
variadic functions
Vari… what? Variadic means that you can define a function to take as many parameters as you like without having to use the func_num_args
and func_get_arg
. You define a last parameter in your function declaration and all parameters that are given “after” that parameter will be summed up into an array. Hard to understand? Look at this example:
Old style:
function foo($a, $b) {
$c = array();
if (func_num_args() > 2) {
for($i = 0; $i < func_num_args()-2; $i++) {
$c[] = func_get_arg($i+2);
}
}
var_dump($a, $b, $c)
// Do something
}
foo('a', 'b', 'c', 'd');
New style;
function foo($a, $b, …$c) {
var_dump($a, $b, $c);
// Do something
}
foo('a', 'b', 'c', 'd');
Both will print 'a', 'b', ['c', 'd']
Argument unpacking
This one complements the previous variadic functions as it allows to call a function with either multiple parameters or an array or iterator returning the parameters.
Old style:
strpos('test', 't');
New style:
strpos(…['test', 't']);
PHPDBG
With phpdbg a SAPI-module for debugging via CLI and remote comes directly bundled with the PHP-Binaries. It allows direct interaction with the code so it comes in quite handy. It is not a replacement of XDebug but a different tool! For more information have a look at the phpdbg-Homepage
Streams for raw POST-Data
This is one of the changes “under-the-hood” which might save a lot of memory. Instead of providing the raw content of the POST-body directly in a variable it’s content will be provided via a stream-resource. By default currently the $GLOBALS[HTTP_RAW_POST_DATA]
will not be initialized any more and you can access the raw POST-data as follows:
$GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents('php://input');
Default character encoding improvements
Ever tried to set the correct default-encoding in PHP? Was it php.input_encoding
or php.internal_encoding
? Or mbstring.internal_encoding
or iconv.internal_encoding
?
Forget all about it. In PHP 5.6 you can set default_charset
and that will be the default for everything else. Even the functions that can take an encoding as argument take that encoding as default. So to have phpun you should set that to something universally available as SJIS-mac
(Mac Japanese)
Oh, and to make things easier in the future old iconv.* and mbstring.* settings in php.ini
will raise E_DEPRECATED error as they will be removed in PHP_next.
TLS improvements
One improvement is to verify the peer when opening a secure connection via a stream by default. For that it is possible to specify your own cafile
and capath
via new php.ini settings. This can also be done on runtime or via a stream-resource.
It has been possible in other versions of PHP via a stream resource, but not by default.
Old style:
$uri = 'https://www.bankofamerica.com/';
$ctx = stream_context_create(['ssl' => [
'verify_peer' => true,
'cafile' => '/hard/path/to/cacert.pem',
'CN_match' => 'www.bankofamerica.com'
]]);
$html = file_get_contents($uri, FALSE, $ctx); // okay, we're good here
New style:
$uri = 'https://www.bankofamerica.com/';
$html = file_get_contents($uri); // okay, we're good here
The other improvements complement the default peer verification by introducing some sane and secure default values for TLS-related functions. These include a stronger default cipher list, introduction of tls1.1 and tls1.2 as stream-wrappers and an easy way to expose the default cert-paths.
More “under-the-hood”-Impriovements
There are many more improvements like raising an E_NOTICE error when using crypt
without a salt or ways of internal operator overloading in GMP that are not directly affecting or do not have an outstandig effect on userland code. For more information again have a look at the RFC-Page
I hope that I got everything correctly. If I misinterpreted something or am talking plain codswallow feel free to tell me 😉