PHP 7 Features & Improvements

PHP 7 Features

PHP 7 Features

PHP 7 is the newest version of PHP released last year. PHP community has introduced a lot of new features in their latest version. In this tutorial, We’ll learn about PHP 7 features and their improvements.

The great thing about PHP 7 is their speed. It is based on the PHPNG(PHP Next-Gen) project , that was led by Zend to speed up PHP applications. As per benchmark presented by Zend performance team, The performance gains realized from PHP 7 are huge! It means your PHP code will execute much faster and take less memory as compared to previous versions.

How to install PHP 7 on Ubuntu

Let’s go through the new features introduced by PHP community in PHP 7.

PHP 7 Features & Improvements


Scalar Type Hints in PHP 7

What is Type Hints ?

Type hints (or type declarations) refers to which type of variable you are going to set. PHP is a weakly typed language, it means it doesn’t force you to declare data types. Type Hints help you to specify what kind of a variable you expect in your function.

Before PHP 7 you can type hints only classes, arrays and callables.

First major improvement in PHP 7 is you can use type hints for scalar data types (such as integers, floats, strings, and Booleans ).

Subscribe Our Tutorials

Get Latest Updates on Facebook

Let’s understand this through example –

I have created a class Employee and in this class, I am assigning two values age of type integer and the name is of type string.

Let’s create an object.

Everything works fine as we pass desired data type. Let’s pass a string in age argument.

It will throw an exception.

NOTE – By default, scalar type hints are non-restrictive. It means your type declaration will not work until you make them restrictive.

To make scalar type hints restrictive declare the following line on the top of your code.


Return Type Declaration in PHP 7

Scalar type hints ensure input consistency, Similarly return type declaration ensure output consistency.

Return type can be declared with :data-type. Let’s understand through an example.

Like scalar type hints, By default return type declaration is also non-restrictive. To make them restrictive we have to enable strict type.

PHP Traits

Group Use Declaration for Namespace in PHP 7

In PHP 7, Namespace implementation is improved by introducing the concept of Group Use. For large codebase, Group use Declarations are more readable and makes it easier to import classes, constants, and functions in a concise way.

Let’s suppose we have created three classes Class A, Class B and Class C in namespace App\Models.

To use them theses three classes in our code we write code,

Before PHP 7

PHP 7 Code

Group use declarations make it easier and less verbose to import multiple structures from a common namespace.

Late static binding in PHP

Null Coalesce Operator in PHP 7

The null coalesce operator is a shorthand notation for checking whether a variable is set and not null.

Example –

Before PHP 7


About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , , . Bookmark the permalink.