Some cool PHP features you can use now that we are on 5.6

Hi all. I thought I'd list out a few features you can take advantage of now that we are on PHP 5.6.

The ::class suffix

You can reference the name of a class by specifying its name with ::class as its suffix.

use Vanilla;

echo Addon::class; // prints "Vanilla\Addon"

You can see here that the fully qualified class name is printed out when you use the ::class suffix. This is a small one that will become a bit nicer when with our increased use of namespaces and dependency injection. You may not run into it in practice, but I find that I've had to specify full class names quite a bit in unit tests and not being able to use the suffix is a pain.

Variadic functions

You can now specify functions that take a variable list of arguments using ... notation in the function rather than having to use funct_get_args().

function sum(...$numbers) {
    $acc = 0;
    foreach ($numbers as $n) {
        $acc += $n;
    }
    return $acc;
}

You can also use ... when calling functions to spread an array (or Traversable!) out. This is sometimes called the "splat" operator.

echo sprintf('Hey %s, you %s!', ...['Todd', 'rule']);

You might notice that if PHP had this functionality they wouldn't need functions like call_user_func_array(). Recently, I've had the case where this would have been really handy to use in a constructor where call_user_func_array() doesn't work. I'd say that these two operators mean we should think about creating variadic functions more.

Generators

Generators allow you to write a function that can be iterated over. So instead of writing a function that returns an array you can write a generator.

function xrange($start, $limit, $step = 1) {
    for ($i = $start; $i <= $limit; $i += $step) {
        yield $i;
    }
}

Generators are great when you would otherwise need to return a massive array that takes up a lot of memory. One thing I'll say is don't dive into using generators for anything you can think of. In most cases returning a simple array is better. In Vanilla's case I can imagine our route for looking up asset locations would work well with generators.

The finally keyword

You can now specify finally in a try..catch block. Code in the finally block will always be called, regardless of whether an exception occurs. You use finally to clean up resources. Since PHP does this mostly automatically you don't usually have to use finally, but when you do it's the right way to do things.

try {
   $fp = fopen($path);
   // ...
} finally {
    fclose($fp); // always called
}

Other languages have had this feature forever. Now PHP finally has it too.

The empty() construct now works on expressions

We would have a lot of bugs on older PHP because of this. Now you don't have to worry so much about using empty(). One thing to note that empty() is often the right thing to do instead of testing a variable directly in an if statement.

if ($var) {

}

if (!empty($var)) {
}

The two statements above behave the same, but the first version usually gets a wrist slap from code analysis tools because you are treating a variable as a boolean even though it may not be one. Using PHP as though it is strongly typed as much as possible is a good practice.

Look up new features with each release

We moved from PHP 5.4 to 5.6 so we have two versions of new features. The stuff I put above is just what I think is the most useful, but there's more Whenever a new release comes out I always look at the upgrade notes so I can see what we'll be able to take advantage of. I recommend you do this yourself too. Sometimes a little bit of syntactic sugar can open up a lot of ideas.