PHP 7.1 new features

Nullable Types

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

Void Function

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

Symmetric Array Destructuring

$data = [
	[1, 'TestB'],
	[2, 'TestC'],
];
[0,'TestA'] = $data[0];

foreach ($data as [$id, $name]) {
 // logic here with $id and $name
}

Class Constant Visibility

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

function iterator(iterable $iter)
{
	foreach ($iter as $val) {
		//
	}
}

Multi Catch Exception Handling

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

Support for Keys In list()

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

Support for Negative String Offsets

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

Convert Callables to Closures

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

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

PHP 7.1 curl_multi_setopt() add below

New Functions and Constants Introduced

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

https://www.cloudways.com/blog/whats-new-in-php-7-1/