PHP 7 Features Explained with Code Examples

PHP 7 Features Explained with Code Examples.

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 are going to learn about new features added in PHP 7.

PHP 7 Features Explained with Code Examples

PHP 7 Features Explained with Code Examples

The great thing about PHP 7 is their speed. It is based on the PHPNG(PHP Next-Gen) project, that was led by Zend team 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

How to install LAMP on ubuntu

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

PHP 7 Features

 

PHP 7 Scalar Type Hints

What is Type Hints?

Type hints (or Type declarations) enforce expected data type of an argument in a function declaration. If the function argument is of the incorrect type, then an error is generated.

PHP is a weakly typed language, it means it doesn’t force you to declare data types. Type hints allow you to enforce what kind of a data type you expect in a function.

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

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

Let’s understand scalar type hints 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.

Subscribe Our Tutorials

Get Latest Updates on Facebook

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.

 

PHP 7 Return Type Declaration

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

Return type can be declared with :data-type. Let’s understand return type declaration 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

Abstract class Vs interface in PHP

PHP 7 Group Use Declaration for Namespace

In PHP 7, Namespace implementation is improved by introducing the concept of Group Use. For larger 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 import namespace like,

Before PHP 7

PHP 7 Code

In PHP 7, we can import these three classes using namespace in a single statement.

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

Late static binding in PHP

PHP 7 Null Coalesce Operator (??)

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

Example –

Before PHP 7

PHP 7

PHP 7 Spaceship Operator (<=>)

PHP 7 spaceship operator is used for comparing two expressions.

Example –

PHP 7 Constant Arrays

In PHP 7, we can define array constants using define() method. In the previous version of PHP, we can define them with const keyword.

PHP shorthand array declaration

For reference read php documentation

About WebRewrite

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