Saturday, January 9, 2016, 4 mins 21 secs

Emergence of PHP7

Vivek Pandey in Engineering

Finally!! PHP7 is here after a long long wait, it’s almost been eleven years since the last version ie. PHP5.0 got released into the market now. And here we have PHP7 with a lot more than what we can think of.
Let’s discuss each and every feature that would make a difference in our lives 🙂

This is one of the best things about PHP7 wherein we have our latest Next Generation Engine PHPNG ( completely replacing the traditional Zend Engine and is also twice as fast as the PHP5.6 Zend Engine (

Deprecated Items
Some of the things like the ASP style tags (<%, <%= and %>) and the script tags (script language=”php”>) are not a part of PHP7. However, make sure you use the recommended php tag instead.

Fatal Error with multiple “default” clauses
Existing fatal and recoverable fatal errors were replaced by exceptions, making it possible to catch and handle errors, in simple terms- this was done in order to display them in a better way, or for logging them, or even for performing recovery procedures.

Scalar Type Hints
Its allows developers to ensure a better input consistency of a function/method interface.
By default “coercive mode” is enabled. This restricts PHP from throwing a ‘typeerror’ when the types doesn’t exactly match, but when a conversion is still possible.
If you enable “strict mode” a type error is thrown when the signatures don’t match

* Scalar type declarations
function add(int $a, int $b) {
   return $a + $b;

Return Type declarations
The type hints ensure input consistency whereas the return type declarations ensure output consistency. We use a colon before the opening curly brace of a function to hint the return type.

* Return type declarations
function add(int $a, int $b): int{
   return (string)($a + $b);

Anonymous Classes
Anonymous classes are often useful for simple one-off objects. With anonymous classes you can also define a class and instantiate an object inline.

* Anonymous classes
$foo = new class {
   public function foo() {
       return "bar";

The Closure::call() method
Closures are anonymous functions that are declared inline and assigned to a variable. It can be used as a callback for executing later. In PHP 5 it was already possible to bind an object to the scope of the closure as if it was a method. In PHP7 the “call” method has been introduced to simplify the process.

* Closure::call()
class Foo
   private $foo = 'bar';
$getFooCallback = function() {
   return $this->foo;
//PHP5 style
$binding = $getFooCallback->bindTo(new Foo,'Foo');
echo $binding().PHP_EOL;
//PHP7 style
echo $getFooCallback->call(new Foo).PHP_EOL;

The spaceship operator
The so-called “spaceship operator” makes it easier to compare values. Instead of returning a typical true/false value, the spaceship operator returns one of the following values based on the result of the evaluation:
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

* Space ship operator
$array = [
   "1 <=> 1" => 1 <=> 1,
   "1 <=> 2" =>1 <=> 2,
   "2 <=> 1" => 2 <=> 1

The Integer division function
This may not be the most important feature in PHP7, but still it’s worth mentioning for the common good. The intdiv() function returns the integer value of a division whereas regular divisions would return a float value.

* Integer division
   intdiv(10, 3),

Group use Declarations
This gives the ability to group multiple use declarations according to the parent namespace. This seeks to remove code verbosity when importing multiple classes, functions, or constants that come under the same namespace.

// Pre PHP7 code
use somenamespaceClassA;
use somenamespaceClassB;
use somenamespaceClassC as C;

use function somenamespacefn_a;
use function somenamespacefn_b;
use function somenamespacefn_c;

use const somenamespaceConstA;
use const somenamespaceConstB;
use const somenamespaceConstC;

// PHP7+ code
use somenamespace{ClassA, ClassB, ClassC as C};
use function somenamespace{fn_a, fn_b, fn_c};
use const somenamespace{ConstA, ConstB, ConstC};

CSPRNG Functions
This feature introduces two new functions for generating cryptographically secure integers and strings. They expose simple APIs and are platform-independent.
Function signatures:

string random_bytes(int length);
int random_int(int min, int max);

Both functions will emit an Error exception if a source of sufficient randomness cannot be found.

Support for Array Constants in define()
The ability to define array constants was introduced in PHP5.6 using the ‘const’ keyword. This ability has now been extended to the define() function too.

define('ALLOWED_IMAGE_EXTENSIONS', ['jpg', 'jpeg', 'gif', 'png']);

JSON Extension Replaced with JSOND
The licensing of the old JSON extension was regarded as non-free, this was causing issues for many Linux-based distributions. The extension has now been replaced with JSOND and this comes with some performance gains and backward compatibility breakages.

Uniform variable syntax
In PHP7 the “uniform variable syntax” has been introduced. This changes the way in which the indirect access to array keys, properties and methods are evaluated. The PHP7 interpretation enforces a strict left-to-right evaluation.

With the above pointers, I hope you have had a good dosage of the extended features with PHP7. Have a great time working with it! 🙂

Vivek Pandey

Vivek Pandey