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.
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
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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Class Employee { private $age; private $name; //Epect age of type integer and name of type string public function __construct(int $age, string $name) { $this->age = $age; $this->name = $name; } public function getAge() { return $this->age; } public function getName() { return $this->name; } } |
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.
1 2 3 |
$employee = new Employee(10, 'raj'); echo $employee->getAge(); // 10 |
Everything works fine as we pass desired data type. Let’s pass a string in age argument.
1 2 3 4 5 |
// Pass string type in int declaration $employee = new Employee('ten', 'raj'); echo $employee->getAge(); |
It will throw an exception.
1 2 3 4 5 |
Fatal error: Uncaught TypeError: Argument 1 passed to Employee::__construct() must be of the type integer, string given, called in /var/www/html/index.php on line 30 and defined in /var/www/html/index.php:9 Stack trace: #0 /var/www/html/index.php(30): Employee->__construct('ten', 'raj') #1 {main} thrown in /var/www/html/index.php on line 9 |
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.
1 |
declare(strict_types = 1); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
Class Employee { private $age; private $name; public function __construct(int $age, string $name) { $this->age = $age; $this->name = $name; } // Return type declaration (Return int) public function getAge() : int { return $this->age; } // Return a string value public function getName() : string { return $this->name; } } |
Like scalar type hints, By default return type declaration is also non-restrictive. To make them restrictive we have to enable strict type.
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
1 2 3 |
use App\Models\ClassA; use App\Models\ClassB; use App\Models\ClassC; |
PHP 7 Code
In PHP 7, we can import these three classes using namespace in a single statement.
1 |
use App\Models\{ClassA, ClassB, ClassC}; |
Group use declarations make it easier and less verbose to import multiple structures from a common namespace.
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
1 2 3 4 5 6 7 8 9 |
echo isset($a) ? $a : 0; // Output - 0 $product = "Nokia"; echo isset($product) ? $product : ''; // Output - Nokia |
PHP 7
1 2 3 4 5 6 7 8 9 |
echo $a ?? 0; // Output - 0 $product = "Nokia"; echo $product ?? ''; // Output - Nokia |
PHP 7 Spaceship Operator (<=>)
PHP 7 spaceship operator is used for comparing two expressions.
1 2 3 4 5 |
echo $a <=> $b; Return 0 if values are equal Return 1 if value on the left side is greater Return -1 if the value on the right side is greater |
Example –
1 2 3 |
echo 5 <=> 5; // return 0 echo 2 <=> 5; // return -1 echo 5 <=> 2; // return 1 |
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
1 2 3 4 5 6 7 8 9 |
<?php define('mobiles', [ 'nokia', 'samsung', 'htc' ]); echo mobiles[1]; //output - samsung ?> |
For reference read php documentation