Five Brand-spanking New PHP 7 Features Every Developer Needs to Know

Friday Aug 25th 2017

Learn what's new in PHP 7, and why you need to start using it.

By Eugene Rudenko.

The last time a new version of new PHP came out, in 2004, the Web and scripting demands were a lot different than they are today. So, we have a right to expect big things—and we're not disappointed. PHP 7 has some awesome features, but first—what happened to PHP 6?

In development since 2005, PHP 6 was hamstrung by UTF-16 and Unicode issues. The parts of it that weren't Unicode-dependent were independently released in 2010 as PHP 5.4 and a new, much improved PHP 7 was scheduled for release in 2015.

So what's changed?

Big alterations include shifts in how the PHP Zend engine handles data structures, and a major focus on Web performance, which have nearly doubled the performance of WordPress sites.

There also have been major advances in support for operating systems, including 64-bit Windows support, and new operators within the language itself.

Let's break down exactly how different PHP 7 is, and what that means for developers.

1. PHP 7 Performance

PHP 7 is built on the NGPHP—'Next Generation PHP'—branch, created by Dmitry Stogov, Xinchen Hui, and Nikita Popov. Now, it's referred to as the Zend 3 engine, distinguishing it from the Zend 2 it replaces.

Zend 3 refactors the Zend engine to use more compact data structures, fewer heap allocations and deallocations, and improved cache locality. It retains near-complete language compatibility, so the new Zend Engine 3 is all upside.

Zend 3 provides radical acceleration: doubling or even tripling speeds compared to PHP 5 is not unheard of, and can be achieved without changing a line of code as well as in all new code going forward.

Key Web functionality is radically accelerated with PHP 7, including WordPress:

PHP 7 accelerates WordPress
Figure 1: PHP 7 accelerates WordPress


Magneto also is accelerated
Figure 2: Magneto also is accelerated

… and even CRM functionality:

And, CRM functionality is improved
Figure 3: And, CRM functionality is improved

2. PHP 7 Error Handling

PHP 7 has improved error handling, relieving a major headache for PHP coders. Now, fatal errors and catchable fatal errors can be replaced with exceptions by the Exception Engine.

Note: If the exception isn't caught, PHP 7 will return the same fatal errors as PHP 5.

Because the new Exception Engine exceptions don't extend the Exception base class, there are now two different types of exceptions in PHP 7: Engine Exceptions and traditional exceptions. To let programmers catch both, early versions of PHP 7 used a shared parent class: BaseException.

You got a hierarchy like this:

Hierarchy of BaseException
Figure 4: Hierarchy of BaseException

This meant that Engine Exceptions didn't get caught up in 'catch (\Exception $e)' catch-all blocks.

Since alpha2 PHP 7, this has changed. Now, Engine exceptions have lost their \Exception suffix and become \Error and \*Error exceptions. The hierarchy now looks like this:

Throwable interface chart
Figure 5: Throwable interface chart

Note: This still leaves PHP programmers unable to create their own error and exception hierarchies—instead, you'll have to extend Exception or Error exceptions.

This is both a feature and a backwards compatibility break. In PHP 7, errors don't behave the way they used to. The interface is similar to exceptions.

3. PHP 7 Anonymous Classes

PHP 7 lets programmers instantiate anonymous classes on the fly, something that C# and Java programmers have been free to do for a long time. An anonymous class is simply a class without a name, and the object it instantiates has the same functionality that objects of named classes do.

Creating anonymous classes is simple: They use the same syntax as named classes. They're an excellent choice to speed up both coding and execution, if the class doesn't need to be documented or will be used only once during execution.

As this example shows, to create an anonymous class, use the same syntax you'd use to create a traditional, named class, only with the name missing:

Creating an anonymous class
Figure 6: Creating an anonymous class

4. PHP 7 New Operators

PHP 7 comes with powerful new operators, suggested by popular features of other programming languages.

Spaceship operators allow PHP 7 users to compare two expressions, returning 0 if both operands are equal, +1 if the one on the left is greater, and -1 if the one on the right is greater. You'll get 0, +1, or -1 regardless of the actual amount of difference between the operands.

A spaceship operator is technically known as the 'Combined Comparison Operator,' but the resemblance of its notation to a simplified flying saucer <=> means it's likely to keep the 'spaceship' moniker. If you've ever used Perl or Ruby, you'll recognize this type of three-way operator.

Some extant PHP operators have spaceship equivalents:

Spaceship equivalency chart
Figure 7: Spaceship equivalency chart

In action, it looks like this:

Using the equivalencies
Figure 8: Using the equivalencies

The Null coalescing operator uses two question marks as its notation: (??). Use it when you want to check whether something exists, and return a default value in case the thing you're searching for doesn't exist. It returns the the result of its first operand if it exists and isn't null, and a second operand in all other cases. That means you won't get an E_NOTICE when you use $_GET['mykey'] ?? "".

It works like this:

Using the Null coalescing operator
Figure 9: Using the Null coalescing operator

5. PHP 7 New Declarations

New declarations include scalar type and return type declarations, major new features in PHP 7.

For scalar type declarations, by default, PHP 7 runs in coercive mode and forces strings to be integers.

Running in coercive mode
Figure 10: Running in coercive mode

That means if you enter numbers in the wrong, way they'll be converted automatically. You can run PHP 7 in strict mode, too, by placing a 'declare ()' directive at the top of the file. Doing this also affects return type.

Running in strict mode
Figure 11: Running in strict mode

New scalar type declarations let you enforce new types for parameters, either coercively or strictly, including: strings (string), integers (int), floating-point numbers (float), and booleans (bool). Because these augment other types introduced throughout PHP 5, including class names, interfaces, array, and callable, they affect class names, and you can't use classes named int, string, float, and bool in PHP 7.

Return type declarations let you specify the return type of a function, method, or closure. Supported return types include string, int, float, bool, array, callable, self (methods only), parent (methods only), closure, the name of a class, and the name of an interface.

Specifying the return type
Figure 12: Specifying the return type

With regard to return type, PHP 7 uses nonvariance. When a method is overridden in a subtyped class, or implemented as defined in a contract, its return type and the method it implements or re-implements must exactly match:

Overriding a method
Figure 13: Overriding a method


PHP 7 takes tools that have been key features of other programming languages, and puts them into the hands of the PHP programmer. If you're in Web development, some of this is probably stuff that you've been crying out for. At the same time, reclassification means some of the ground is shifting under the PHP developer. To take advantage of some of the gains PHP 7 offers, you'll have to reconsider some of your go-to moves because some basic actions get reclassified or become forbidden to make space for improvements. Still, it's hard to argue with that 'twice as fast' aspect, though!

About the Author

Eugene Rudenko is a senior online marketing manager at Oxagile, a custom software development company with years of experience in custom Web application development and strong mobile app development expertise.

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved