# Getting ready for PHP 5.6

PHP 5.6.0, currently in alpha, will hit us any time later this year. Time to take a look and see what’s new. Here’s a selection of features that will affect the way you and I code.

## New __debugInfo() magic function (rfc)

This new magic function allows you to instruct PHP what it should output when using var_dump(). Take this simple class for example:

class File {
private $fp; public function __debugInfo() { return$this->fp ? stream_get_meta_data($fp) : []; } public function open($filename, $mode = 'r'){$this->fp = fopen($filename,$mode);
}
}

In PHP < 5.6 you’d get back a simple dump of all the datamembers the class holds. This is not always helpful, as seen in the example below:

$f = new File();$f->open('http://php.net');
var_dump($f); object(File)#1 (1) { ["fp":"File":private]=> resource(5) of type (stream) } With PHP 5.6 the __debugInfo() function will kick in, and send back that result to var_dump(): $f = new File();
$f->open('http://php.net'); var_dump($f);

object(File)#1 {
["wrapper_type"]=>
string(4) "http"
["stream_type"]=>
string(10) "tcp_socket"
…
}

Note that you can safely use this function PHP < 5.6, as older versions of PHP will just ignore it.

~

## Power Operator Support (rfc)

So we all know +, -, *, /, ., etc. right? Well, there’s a new kid in town: **, which is PHP’s exponential operator:

echo 2 ** 4; // 16

Beware: it’s right associative, meaning the expression on the right will be interpreted first when using multiple operators:

echo 2 ** 3 ** 2; // 512 (not 64)

~

## Argument Unpacking (rfc)

Arrays (and Traversables) can now be unpacked using the ... operator (splat operator), which is prefixed to the variable. This will allow you to remove all those call_user_func_array() calls (if you have any).

Pre PHP 5.6:

call_user_func_array([$db, 'query'], array_merge(array($query), $params)); With PHP 5.6, using the splat operator: $db->query($query, ...$params);

Here’s an other simple example:

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

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

~

Pre PHP 5.6 there’s no way to syntactically spot a variadic function (= a function that has a variable number of arguments). Above that you had to do some funky stuff using array_slice() in order to get the remaining arguments:

class MySQL implements DB {
protected $pdo; public function query($query) {
$stmt =$this->pdo->prepare($query);$stmt->execute(array_slice(func_get_args(), 1));
return $stmt; } // ... }$userData = $db->query('SELECT * FROM users WHERE id = ?',$userID)->fetch();

PHP 5.6 sports a syntax for variadic functions: pass in an extra parameter and prepend it with aforementioned splat operator (...). Next to being able to spot a variadic by just looking at the syntax, you’ll also able to quickly access the remaining function parameters, as the variable will already be unpacked:

class MySQL implements DB {
public function query($query, ...$params) {
$stmt =$this->pdo->prepare($query);$stmt->execute($params); return$stmt;
}
// ...
}

$userData =$db->query('SELECT * FROM users WHERE id = ?', $userID)->fetch(); ~ ## Constant Scalar Expressions (rfc) The problem with constants in PHP pre PHP 5.6 is that they’re – well … – constant. They must be a fixed value, not a computed one. In PHP 5.6 this will change. Constants as defined below are now accepted: const A = 1 + 1; const B = 2 ** A; const HELLO = "HELLO"; const HELLO_WORLD = HELLO . " WORLD"; ~ ## Bundled phpdbg (rfc) phpdbg is an interactive PHP debugger. It’s a single executable which you run at the terminal. Once booted you instruct PHP where to break (be it on a line number, at a function call, etc.). Take this sample.php for example: <?php$a = 1 + 2;
echo $a; Debug it using phpdbg as follows: [email protected]:~$ phpdbg  -e sample.php
[Welcome to phpdbg, the interactive PHP debugger, v0.3.0]
To get help using phpdbg type "help" and press enter
phpdbg> compile
[Attempting compilation of /home/yoya/sample.php]
[Success]
phpdbg> print exec
[Context /home/yoya/sample.php]
L0-0 {main}() /home/yoya/sample.php
L2      0x7f6c901d1c60 ZEND_ADD                       C0                   C1                   @0
L2      0x7f6c901d1c90 ZEND_ASSIGN                    $a @0 @1 L3 0x7f6c901d1cc0 ZEND_ECHO$a                   <unused>             <unused>
phpdbg> break line 3
[Breakpoint #0 added at line 3]
phpdbg> run
3phpdbg>

Alternatively you can break from within your PHP code using phpdbg_break(). Also note that the compile step is not mandatory.

Once a breakpoint was reached you can inspect the current environment, agina using commands such as list, print, info, etc. Other familiar debug commands such as step, next, leave, etc. are also available.

phpdbg also allows you to remotely debug. Mocking a server is also possible.

The next version of PHPStorm will use phpdbg as the default debugger. Also pretty sure someone on the interwebs will write a nice standalone GUI for this if you’re not that into CLI mode nor PHPStorm.

~

## Importing namespaced functions (rfc)

With PHP 5.6 the use operator will be extended to also import functions and constants using use function and use const:

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

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

echo FOO . PHP_EOL; // 42
f(); // Name\Space\f
}

Note that you still won’t be able to import an entire namespace using the use operator. You’ll still have to reside to aliasing the namespace (and using that alias further down):

<?php
namespace foo\bar {
function baz() {
return 'foo.bar.baz';
}
}

namespace {
use foo\bar as b;
var_dump(b\baz());
}

~

Next to these new syntactic features, a few other things – such as SSL/TLS improvements, Large file uploads, and a reusable php://input – will also land in PHP 5.6.

Consider donating.

I don’t run ads on my blog nor do I do this for profit. A donation however would always put a smile on my face though. Thanks!

Note: most examples are taken from the RFCs. You can find links to the RFCs next to each title. The phpdbg example was taken from here. Header image courtesy Dev-Metal

Bramus is a Freelance Web Developer from Belgium. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)