H

June 25, 2015

Lately I've been reading a lot of articles on PHP 7, which is supposed to land around November this year. There's a sweet cheat sheet for the new features, available on github. Here are a few features I'm particularly excited about:

Null Coalesce Operator

The null coalesce operator (or isset ternary operator) is a shorthand notation for performing isset() checks in the ternary operator. This is a common thing to do in applications, and so a new syntax has been introduced for this exact purpose.
// Pre PHP 7 code
$route = isset($_GET['route']) ? $_GET['route'] : 'index';

// PHP 7+ code
$route = $_GET['route'] ?? 'index';

Awesome! I write these isset ternary operators quite often and now my code will look so much cleaner.

Scalar Type Declarations

Scalar type declarations come in two flavours: coercive (default) and strict. The following types for parameters can now be enforced (either coercively or strictly): strings (string), integers (int), floating-point numbers (float), and booleans (bool). They augment the other types introduced in the PHP 5.x versions: class names, interfaces, array and callable.
function addOne(int $value): int {
	return 1 + $value;
}

var_dump(addOne(3)); // int(4)

I've always wanted to have this capability. I mean, I cast array and class name parameters all the time—I've always felt dirty not doing the same with the rest. Also I snuck in the Return Type Declaration here, which is also cool but pretty self-explanatory.

Group use Declarations

This gives the ability to group multiple use declarations according to the parent namespace. This seeks to remove code verbosity when importing multiple classes, functions, or constants that come under the same namespace.
// Pre PHP 7 code
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Capsule\Manager as Capsule;

// PHP 7+ code
use Illuminate\Database\{Eloquent\Model, Capsule\Manager as Capsule};

This one is pretty sweet. Although I would like to know if you can put the arguments on separate lines, for version control purposes.

Final Thoughts

Overall PHP 7 will breathe new life into the community and allow developers to create faster (~100% increase in performance, rendering HHVM obsolete) and more stable applications with all the new features that make it more than a scripting language. I patiently await November's arrival to spin up a new Amazon EC2 instance running the latest stable.

Next up: Lake George Weekend

Proceed