A Sneak Peek into PHP 7

0
4572

PHP 7 Visual

PHP 7 is a new major version release, arriving 11 years after PHP 5. The scheduled release is before the end of the year. What will this new version have in its repertoire? This article gives the reader a taste of the things to come.

The good news for PHP developers is that PHP 7 RC5 (Release Candidate) is released on October 15, 2015. PHP 7 is in the development phase, so do not use it in a production environment until the final version is out in November. To check when the next beta version will be released, visit https://wiki.php.net/todo/php70#timetable. The current stable version is PHP 5.6.14.
Let’s now check out some of PHP 7’s exciting new features.

The name and new Zend Engine
PHP developers have decided to choose version 7 instead of version 6 to avoid the confusion with past experimental projects. PHP 6 started as an experimental process earlier with a specific set of features. However, it did not reach the production phase. Hence, the developers decided to omit version 6 and instead name their new major release PHP 7.
The Zend Engine has been a core part of PHP since PHP 4. It’s an open source execution engine that interprets the PHP language. With the PHP 5 series, the Zend Engine 2 was introduced with a built-in object model and performance enhancing capabilities. PHP 7 comes with the new engine version called PHPNG (Next Generation).

Improved performance
Though the current stable PHP 5.x series is working significantly faster than all its previous versions, the main goal of PHPNG is to get a much better and improved performance compared to the current versions. PHP 7 with the new engine has remarkably optimised memory usage and the just-in-time compiler does the compilation at runtime rather than prior to execution. Figures 1, 2 and 3 show the performance benchmarks provided by the Zend performance team.
[Reference: http://www.zend.com/en/resources/php7_infographic]

Figure 1_wordpress on PHP 7
Figure 1: WordPress on PHP7

64-bit support
Though a part of the LAMP (Linux, Apache, MySQL, PHP) stack, PHP 7 introduces 64-bit support on Windows systems. It allows the running of both native 64-bit integers and large files on Windows systems.

New operators
PHP 7 comes with two new operators — the null coalesce operator and the combined comparison operator.
The null coalesce operator is denoted by double question marks (??). It returns the first operand if it exists and is not null; otherwise, it returns the second operand. It has the same usage as ternary operators in the C language.

<?php
$a = 10;
$b = $a ?? 20;
//Equivalent to: $b = isset($a) ? 10 : 20
//isset() returns true if variable is set and not null.

The combined comparison operator, also known as the spaceship operator, is denoted by <=>. It returns zero if both the operands are equal, 1 if the left operand is greater and -1 if the right operand is greater.

<?php
$a <=> $b

The above expression will be resolved to 0 if $a equals $b, 1 if $a is greater than $b and -1 if $b is greater.

Type declarations
Another feature introduced is to define the return type of functions and methods to avoid unintended return values. The code below states that MyFunction() returns a value which will be converted to bool automatically.

<?php
function MyFunction() : bool
{
return 1;
}
echo MyFunction();

PHP 7 introduces four new types of declarations for scalar types — integer, float, string and Boolean. By default, in a scalar type, if we pass a float value to an integer parameter, it will re-correct it to the integer without outputting any warning or error.

<?php
function add(int $a, int $b) : int
{
return $a + $b;
}
echo add(1, 2); // 3
echo add(1.5, 2.5); // 3 as floats are truncated by default
Figure 2_frameworks performance under PHP 7
Figure 2: Frameworks performance under PHP 7

Anonymous classes
Anonymous class (class without a name) is another feature provided which is already available in Java and other object-oriented languages. It has the same syntax as the normal class that was written in PHP but without a name. It can be used in case the class does not need to be documented.

<?php
var_dump(new class()
{
public function Myfunction()
{
//code
}
});

Import from the same namespace
This Group Use Declaration feature allows us to import many classes from the same namespace with a single line.

<?php
//current use syntax
use MyLibrary\Abc\classTest1
use MyLibrary\Abc\classTest2
use MyLibrary\Abc\classTest3

//group use declaration syntax
use MyLibrary\Abc{classTest1, classTest2, classTest3};
Figure 3_PHP 7 against other dynamic languages
Figure 3: PHP 7 against other dynamic languages

Engine exceptions
The ‘engine exceptions’ feature comes with PHP 7 to facilitate the error handling process in code. It allows us to replace fatal and recoverable fatal errors by exceptions. Catchable errors can be displayed and defined actions can be taken. In case the exception is not caught, PHP returns the fatal error. The \EngineException objects do not extend the \Exception base class. This results in two kinds of exceptions—traditional and engine exceptions.

<?php
function MyFunction($obj)
{
//code
}
MyFunction(null); //Fatal error

Considering the previous code, the code below shows how a fatal error is replaced by an EngineException:

try
{
MyFunction(null);
}
catch(EngineException $ex)
{
echo “Exception Caught”;
}

Lists of deprecated items have been removed from PHP 7 — for instance, ASP style tags (<%=, <%, %>) and script tags (<script language=”php”>), safe mode and its ini directives, and so on. MySQL extension has been deprecated since PHP 5.5, and replaced by the mysqli extension (mysqli_* functions). Likewise, the ereg extension has been deprecated since PHP 5.3, and replaced with the PCRE extension (preg_* functions). Do visit https://wiki.php.net/rfc#php_70 for more information.

References
[1] www.php.net
[2] www.zend.com/ n/resources/php7_infographic

LEAVE A REPLY

Please enter your comment!
Please enter your name here