Top 5 Features Every Developer Needs to Know about PHP 7

PHP 7 has some awesome features, 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 speedscompared 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.

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.

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.

To create an anonymous class, use the same syntax you’d use to create a traditional, named class, only with the name missing.

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.

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.

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.

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.

Conclusion

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!

Leave a Reply

Your email address will not be published. Required fields are marked *