PHP 7: 10 things you need to know (Part 2)

Ngoc Huynh

6. New Spaceship and Null Coalescing Operators

The Spaceship operator runs under the official name of Combined Comparison Operator. The notation of the new operator looks like this: <=> (kind of like a simplified spaceship, if you imagine it right).

The spacehip operator returns 0 if both operands are equal, 1 if the left is greater, and -1 if the right is greater. It’s also called a three-way comparison operator, and it already exists in other popular programming languages like Perl and Ruby.

The Null Coalescing operator is denoted with two question marks ( ?? ). You can use it when you want to check if something exists and return a default value, in case it doesn’t. The coalesce operator returns the result of its first operand if it exists and is not null, and the second operand in any other cases.

Here’s how the new operator reduces the time spent with basic declarations:

7. Enables Accurate Type Declarations

Have you ever wanted to prevent unintended return values by declaring the return type of a function? Well, the new PHP 7 enables developers to enhance the quality of their code with the help of return type declarations.

The image below depicts a very simple use case where the foo() function is supposed to return an array. Check out more complicated examples here.

To enhance the feature even more, PHP 7 introduces 4 new type declarations for scalar types: int, float, string and bool. The new scalar types allow developers to denote that they are expecting integers, floats, strings, or booleans to be returned. The new scalar types introduced by PHP 7 will also be supported by argument Type Hints that enables developers to force the type of parameters since the PHP 5.X series.

8. Adds Anonymous Classes

PHP 7 enables you to use anonymous classes, already a well-established practice in other object-oriented languages like C# and Java. An anonymous class is a class without a name. The object it instantiates has the same functionality as an object of a named class.

The syntax is the same as what we are used to in traditional PHP classes, only the name is missing. If anonymous classes are used well, they can speed up coding as well execution time. Anonymous classes are excellent when a class is used only once during execution and in cases when a class doesn’t need to be documented.

9. Facilitates Imports From the Same Namespace

The new Group Use Declarations feature will be godsent to those of you who want to import many classes from the same namespace. The new syntax cuts verbosity, makes your code tidier and easier on the eyes, and saves you a lot of typing time.

It will also be easier to read through and debug codes, as group use declarations help you identify the imports that belong to the same module.

10. Cleans Up The Room

The goal of PHP 7 was to free up the space to enable improvement, so it was necessary to get rid of many deprecated functionalities and old and unsupported Server APIs and extensions. If you want to check which are these in detail, click here and here.

All the removed items have been deprecated for a while in PHP 5 so most likely you haven’t used them for a long time. However please note if you have a legacy app running on older PHP versions the new PHP 7 can potentially break the code.

Share the news now