PHP 7 Features & Improvements

1. Scalar Type Declaration/Hints

To understand this, you must know the meaning of type declaration or type hints. We all are very familiar with actual and formal arguments. But here we are talking about the type(Data Type) of actual and formal arguments. You can specify the type hint in the function definition. This feature works in two flavors: coercive (default) and strict mode. The following example will make it more clear to you:

?
1
2
3
4
5
6
7
8
9
/**
* Scalar type declarations/hints
*/
//declare(strict_types=1);
function multiply(int $a, int $b) {
return $a * $b;
}
var_dump(add(2,4));
var_dump(add("2","4"));

The coercive mode is enabled by default. This restricts PHP from throwing a type error when the types don’t exactly match, but when a conversion is still possible. This feature ensures the better input consistency of methods.

2. Return Type Declaration/Hints

We are very glad for this feature. Whereas the above PHP 7 feature ensures input consistency, return type declarations ensure output consistency.

?
1
2
3
4
5
6
7
8
/**
* Return type declarations
*/
//declare(strict_types=1);
function multiply(int $a, int $b): int{
return (string)($a * $b);
}
var_dump(multiply(4,2));

Here, if you enable “strict mode” then this code will throw a type error because we have declared return type int but returning the string.

3. Anonymous Classes

As JavaScript provide the feature of anonymous classes, PHP 7 has also introduced this feature. Using anonymous classes you can define a class and instantiate on object inline.

?
1
2
3
4
5
6
7
8
9
/**
* Anonymous classes
*/
$obj= new class {
public function greetings() {
return 'Good morning, Tricky Admin!';
}
};
var_dump($obj,$obj->greetings());

4. The Closure::call() Function

This function is more faster then the formal way. Actually, this is a shorthand way to bind a object scope to a closure and call it. Closure are anonymous functions that re declared inline and assigned to a variable. Its possible in PHP 5 but call method is just introduced to simplify the process.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* Closure::call()
*/
class Greeting
{
private $morning = 'Good morning, Tricky Admin!'.PHP_EOL;
}
$getGreetingCallback = function() {
return $this->morning;
};
//Old PHP 5 style
$binding = $getGreetingCallback->bindTo(new Greeting,'Greeting');
echo $binding();
//PHP 7 style
echo $getGreetingCallback->call(new Greeting);

5. Null Coalesce(??) Operator

The null coalesce operator is an alternative for isset() function. This can be used for inline comparisons to check if a variable is set or not null. With this operator programmers get rid of ternary(?:) operator and code becomes more less.

?
1
2
3
4
5
6
7
$array = ['greeting'=>'Good Morning, Tricky Admin!'];
//PHP5 style
$greet = isset($array['greeting']) ? $array['greeting'] : 'not set';
echo $greet.PHP_EOL;
//PHP7 style
$greet = $array['greeting'] ?? 'not set';
echo $greet;

6. Spaceship(<=>) Operator

This operator is used to compare two values inline. Instead of return true/false, it returns 0, -1, 1 when first variable is respectively equal, less than, or greater than second variable. The following code will make it more clear.

?
1
2
3
4
5
6
7
// 0 when both values are equal
// -1 when the left value is less than the right value
// 1 if the left value is greater than the right value
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

Performance

Its worth to mention here that, PHP 7 is more faster than PHP older versions. Some programmers say that it is two times faster then PHP 5. We are very confident that it perform great and we are curious to see the average numbers once people start using it.

Thanks!