PHP 7.1 New Features

Support for PHP 5.5 has just ended and PHP 7.0 has been released!

The combined impact of these two events have rocked PHP development world. In many instances, PHP developers have started to migrate their projects to PHP 7.0. To make sure that version 7 remains stable and perform as well as its predecessors, PHP 7.1 was released on December 1st, 2016.

Let’s have a look at what’s new in PHP 7.1.

Nullable Types

With the introduction of Nullable Types, you can now return null from a function. This feature was already well supported in other languages including C# and Java. Nullable types are illustrated in the following example:

<?php

function checkAge($age): ?int
{
 if($age > 12){
 return $age;
 }else{
 return null; 
 }
}

?>

Void Function

The much awaited void function has been included inside PHP 7.1. You can now set void as the return type for your functions. For instance:

<?php
function setkey($key) : void
{
 $this->key = $key;
}
?>



Symmetric Array Destructuring

In PHP 7.1, array syntax ([ ]) can be used to destructure arrays for assignment. This is an excellent alternate to list() syntax (which is also available). This structure can be used in a foreach statement for assignment. For instance:

<?php
$data = [
 [1, 'TestB'],
 [2, 'TestC'],
];
[0,'TestA'] = $data[0];
foreach ($data as [$id, $name]) {
 // logic here with $id and $name
}
?>

Class Constant Visibility

You can now use access modifiers with a constant variable within the class to handle its visibility across the project. For instance:

<?php
class ConstDemo
{
 const PUBLIC_CONST_A = 1;
 public const PUBLIC_CONST_B = 2;
 protected const PROTECTED_CONST = 3;
 private const PRIVATE_CONST = 4;
}
?>

Iterable Pseudo-type

PHP 7.1 has introduced a new pseudo-type that is similar to callable. It can be used in parameter and return type. It accepts either, arrays or objects, that implement Traversable interface. For instance:

<?php
function iterator(iterable $iter)
{
	foreach ($iter as $val) {
		//
	}
}
?>

Multi Catch Exception Handling

With the introduction of Multi Catch exception you can now catch more than one exceptions at a time in a single catch. For instance:

<?php
try {
 // some code
} catch (FirstException | SecondException | ThirdException $e) {
 // handle first and second exceptions
}
?>

Support for Keys In list()

You can now specify keys while creating a list using list() function or using new shorthand syntax, i.e. ([ ]). For instance:

<?php
//Shorthand style
$data = [
 ["id" => 1, "name" => 'Tom'],
 ["id" => 2, "name" => 'Fred'],
];
// list() style
list("id" => $id1, "name" => $name1) = $data[0];
?>

Support for Negative String Offsets

You now use negative offsets for string manipulation or can index it with ([ ]) or ({ }). Also, you can use simple variable parsing syntax inside string. For instance:

<?php
var_dump("abcdef"[-2]);
$string = 'bar';
echo "The last character of '$string' is '$string[-1]'.\n";
?>

Convert Callables to Closures

PHP 7.1 will have a new static method Closure::fromCallable() within the Closure class to convert callables to Closure object. For instance:

<?php
class Calls
{
 public function showFunction()
 {
 return Closure::fromCallable([$this, 'privateFunction']);
 }
 private function callbackFunction($a,$b)
 {
 echo $a + $b;
 }
}
$privFunc = (new Calls)->showFunction();
$privFunc(1,2);
?>



Asynchronous Signal Handling

To reduce overhead using ticks, a new function pcntl_async_signals() has been introduced to enable asynchronous signal handling directly. For instance:

<?php
pcntl_async_signals(true); // turn on async signals
pcntl_signal(SIGHUP, function($sig) {
 echo "SIGHUP\n";
});
posix_kill(posix_getpid(), SIGHUP);
?>

 

HTTP/2 server push support in ext/curl

You can now use HTTP/2 push using ext/curl. New constants CURLMOPT_PUSHFUNCTION,CURL_PUSH_OK and CURL_PUSH_DENY have been added in PHP 7.1 for curl_multi_setopt().

New Functions and Constants Introduced

Other than these new features, several new functions and constants have also been introduced in PHP 7.1. Some of new functions introduced are:

  • curl_multi_errno()
  • curl_share_errno()
  • curl_share_strerror()
  • session_create_id()
  • session_gc()

There are many other functions as well that can be found here.

Besides functions, some new constants have also been introduced. They are:

  • CURLMOPT_PUSHFUNCTION
  • CURL_PUSH_OK
  • CURL_PUSH_DENY
  • FILTER_FLAG_EMAIL_UNICODE
  • MT_RAND_PHP

I have listed only some of the major constants introduced. You can find other constants over here.

Migrating From 7.0.x to 7.1.x

Previously, we wrote a small guide on migrating from PHP 5.x.x to PHP 7.0.x. It was a bit complex to make the shift. However, if you have recently moved to PHP 7.0, then it will be easier for you to move to PHP 7.1. But before taking up the migration process, have a look at what features have been deprecated in PHP 7.1 from PHP 7.0:

  • ext/mcrypt has been deprecated in PHP 7.1 and will be removed from PHP 7.2
  • Eval option for mb_ereg_replace() and mb_eregi_replace() functions have been also deprecated.

Several functions have also changed in PHP 7.1 which can be found here. Complete migration guide can be found on PHP official site.

0 0 vote
Article Rating
Share this: