You might have knowledge about active development of the biggest version of PHP7. It takes near about a year for the development. Finally the wait is over and we have brand new version of PHP7 with us. The PHP7 timeline was scheduled by members of core PHP and was release on the end of 2015. As soon as the date of release of PHP7 approached our internal group becomes more and more conscious about details of PHP7. They started working at adding long desired features and removing artifacts. The main focus of this release was on removing functionality deprecated in previous version and improving language consistency.
Basically PHP7 is a revolution that delivers applications that power everything from websites and mobile to enterprise the cloud. PHP7 has fairly enough features for advanced web application development and use of latest resources helps in boosting up the development. Since the release of PHP5 in 2005 it is the biggest revolution. It brings explosive performance improvements, drastically reduced memory consumption, and a host of brand new language feature to make apps soar. In comparison to PHP5.6 it provides two times faster performance and 50% better memory consumption. These features in turn allow you to serve more concurrent users without adding more hardware. PHP7 is the ultimate choice for today’s web developer specifically designed and refactored for today’s workloads.
Changes made in previous version: Here fatal errors of previous version of PHP that are now exceptions in PHP 7.
- Changes to exception handling and errors– Many recoverable and fatal errors have been converted to exceptions. Error class are used to inherit these error exceptions and implements Throwable interfaces by itself. So custom error handlers may not be triggered exceptions because exceptions may be thrown. To use exception handler to work for both PHP5 and PHP7 we should remove the type declaration from the handler. In PHP7 we can simply replace the code exception type declaration with throwable
// PHP 5 era code that will break.
function handler(Exception $e) { … }
set_exception_handler(‘handler’);// PHP 5 and 7 compatible.
function handler($e) { … }// PHP 7 only.
function handler(Throwable $e) { … }
?>
- Exception on failure- When the constructor failed, previously some internal classes will return null or an unstable object. In PHP7 for that case exception is thrown in the same way that user classes already had to.
- Error parsing – Parse Error throws parseError object for the purpose of error parsing. eval() should now include a catch block for error handling that can handle this error.
- E_STRICT notices and severity changes– E_STRICT notices have been reclassified for other levels. E_STRICT is a constant that is retained so that error_reporting(E_All/E_STRICT) will not cause any error.
E_STRICT notices severity changes | |
Situation | New level/behaviour |
Indexing by a resource | E_NOTICE |
Abstract static methods | Notice removed, triggers no error |
“Redefining” a constructor | Notice removed, triggers no error |
Signature mismatch during inheritance | E_WARNING |
Same (compatible) property in two used traits | Notice removed, triggers no error |
Accessing static property non-statically | E_NOTICE |
Only variables should be assigned by reference | E_NOTICE |
Only variables should be passed by reference | E_NOTICE |
Calling non-static methods statically | E_DEPRECATED |
- Variable handling– In PHP7 source files are parsed through abstract syntax tree. Previously there were many limitations
In the parser used in the earlier versions of PHP. But abstract syntax tree has permitted many improvements to the language which were impossible previously. But this in turn provides a backward compatibility breaks as a result of removal of few special cases for consistency reasons.
- Indirect variables , properties and methods– In opposition to previous mix of special cases indirect access to variable , properties and methods will now be evaluated in left- to- right order .The table below shows the changes I the order of evaluation:
Old and new evaluation of indirect expressions | ||
Expression | PHP 5 interpretation | PHP 7 interpretation |
$$foo[‘bar’][‘baz’] | ${$foo[‘bar’][‘baz’]} | ($$foo)[‘bar’][‘baz’] |
$foo->$bar[‘baz’] | $foo->{$bar[‘baz’]} | ($foo->$bar)[‘baz’] |
$foo->$bar[‘baz’]() | $foo->{$bar[‘baz’]}() | ($foo->$bar)[‘baz’]() |
Foo::$bar[‘baz’]() | Foo::{$bar[‘baz’]}() | (Foo::$bar)[‘baz’]() |
To use explicitly the order of evaluation one must write the old right-to-left order with curly braces.This will make the code compatible for both PHP5.x and PHP7.x.
- list() handling– list() will now no longer assign variables in reverse order. The order is now defined previously.Generally it affects only in the case of list() and array[] used in conjunction.
Here is the example:
list($a[], $a[]) = [1, 2];
var_dump($a);
?>
Output of the above example in PHP 5:
array(2) {[0]=>
int(2)
[1]=>
int(1)
}
Output of the above example in PHP 7:
array(2) {[0]=>
int(1)
[1]=>
int(2)
}
Empty list() assignment– In PHP7 Empty list() assignment has been removed.
Previous declaration of empty list():
list() = $a;
list(,,) = $a;
list($x, list(), $y) = $a;
?>
This declaration is no longer applicable in PHP7 .
- Strings– list() now cannot unpack string variables. In spite of that str_split() should be used.
- Array ordering– When elements are automatically created by reference assignment then array ordering gets changed. Here is the example of that case:
$array = [];
$array[“a”] =& $array[“b”];
$array[“b”] = 1;
var_dump($array);
?>
Output of the above example in PHP 5:
array(2) {[“b”]=>
&int(1)
[“a”]=>
&int(1)
}
Output of the above example in PHP 7:
array(2) {[“a”]=>
&int(1)
[“b”]=>
&int(1)
}
Global variables– Now there is no need to use global keyword for variable variables.The previous behaviour is emulated by using curly brace syntax.
function f() {
// Valid in PHP 5 only.
global $$foo->bar;// Valid in PHP 5 and 7.
global ${$foo->bar};
}
?>
- Parenthesis – In PHP7 parenthesis around function parameters doesn’t affect behaviour. While in PHP 5, when functions was called by reference it could quiet strict warnings.
function getArray() {
return [1, 2, 3];
}function squareArray(array &$a) {
foreach ($a as &$v) {
$v **= 2;
}
}// Generates a warning in PHP 7.
squareArray((getArray()));
?>
The above example will output:
Notice: Only variables should be passed by reference in /tmp/test.php on line 13
RETURN TYPES: With huge demand from everywhere finally our favourite language gets its return types. We are finally able to specify return type of a particular function in the form:
function roo(): array {return [];
}
So we got an improvement, but not perfection. With the return types in PHP7 we have the following troubles:
- Here return type doesn’t support scalar values, string, bool, int etc. It means methods and functions supporting such types are still unsigned.
- We will have to use remedy for this by returning instances of wrappers for such values.
- It does not provide multiple return types. For example if your function returns arrays or iterator then there’s no way to indicate that.
Execution Engine :- Let’s discuss general aspects regarding the source execution engine. If we talk about the earlier years of PHP, then Zend engine is powering the PHP since 1999. It was firstly used with PHP4 release. Zend engine is written in C language to interpret PHP. Currently PHP5.x is using Zend Engine II.
PHP 7 our brand new version of PHP ofcourse gets a brand new version of Zend engine. This new version of Zend engine is termed as PHP#NG. Here NG stands for next generation. PHPNG comes to be the core of PHP7. The development team of PHPNG has worked a lot on Zend engine to make it efficient to provide remarkably optimized memory usage. The main advantage of PHPNG over former Zend engine is performance improvement.
SPEED:- So far, we have discussed about source execution engine PHPNG. We have seen that PHPNG provides performance improvement and optimized memory usage. Both of these factors play a major role in the speed.
In PHP7 you can execute the code more faster and to serve the same amount of users it requires few servers.
The graph below show how CMS and different frameworks affects speed.
Leave a comment