Thursday, November 24, 2016

PHP 7 vs PHP

PHP 7 was released in December 2015.
The features of PHP7 are fairly enough for advance web application development and making use of latest resources to boost you development. There are lot of talks about PHP5.7, PHP6 or PHP 7. The main reason behind the PHP7 is PHP6 has never reached to stable version, also there are many resources available about PHP6. So PHP community has chosen PHP5.6 to directly PHP7.
Let us get into some details:-
1. Performance:
You might have heard about Facebook HVVM out there, to provide you maximum performance for PHP based application. So PHPNG (PHP-Next-Gen) started with the idea to provide maximum performance as Facebook HVVM provides. The performance improvement source was led by ZEND to speed up PHP based applications. As per Zend performance improvement is huge! You can find some benchmark against HVVM here. Without changing single piece of code, just upgrading to PHP7 gives you enormous performance. Deprecating many redundant features also part of performance improvement of PHP7.
2.Scalar Type Hints & Return Types:
While many developers would like able to declare return type for the function or method. By default PHP7 will allow developers to declare what kind of return type function is expected to return a value. It is almost similar as Type hinting parameters as below.
Example:
 public function isValidUser(int $id) : bool 
   {
     return isset($this->users[$id]) ? true : false;
   }
Above example is similar as Facebook HACK syntax. In this example bool indicates the function will return only boolean value. You can also have look at many example in RFC here. This allows developers to set expecting stringintfloat or boolean to be passed or returned.
3.Spaceship Operator
PHP7 will introduce a new operator called spaceship operator (<=>) otherwise called combined comparison operator. It can be used mostly in sorting and combined comparison. It works like strcmp() or version_compare().
As per the PHP RFC common uses of sorting as below.
Example:
Before PHP7:

function order_func($a, $b) 
{
  return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
}
then,
After PHP7:

function order_func($a, $b) 
{
  return $a <=> $b;
}
It is a beautiful addition to the PHP version. You can find more example here.
4.Additional Features
Apart from above changes there are few new features added into the PHP7 core. I am listing few major changes below.
  • Abstract Syntax Tree: – Another big change on the core. But most likely to be visible to developers. It makes parsing php code easier, less error, and way to build better things in future.
  • Closure::call() : – This is the new method added into core. It allows variable binding at call time rather pre-binding.
  • Null Coalesce Operator : – The coalesce operator or ?? added to the core, which returns the first operand if exists or null.
Example:
   //Before PHP7:

$id = isset($_GET[‘id’]) ? $_GET[‘id’] : null;

 In PHP7:

$id = $_GET[‘id’] ?? null;  //equivalent of writing old above syntax 
  • Uniform Variable Syntax : – The main deference of existing syntax and new syntax is to keep consistence and complete variable syntax. Allows advance expressions.
Example:
$$foo['bar']['baz']   interpreted as   ($$foo)['bar']['baz']
$foo->$bar['baz']()   interpreted as   ($foo->$bar)['baz']()
  • Unicode Codepoint Escape Syntax: – PHP7 introduced syntax to escape unicode codepoint as below.
Example:
echo "\u{202E}Reversed text"; // outputs ‮Reversed text
echo "\u{1F602}"; // outputs ��
5.Cleanups:
Developers can stop recognizing method name as class name to be a constructor as per PHP4 . It has been controversial early to remove but later has been removed completely as currently we no longer required to warn PHP developer how PHP4 works. This removal added as simplicity.
mysql_* functions are completely removed as extension removed by default.
Multiple default clause support has been removed from the switch case.
Support of ASP tags (<%) and script tags (<script language="php"></script>) has been removed meaning that you can't use those tags to enter php mode.
You can find more, list of deprecated or removed functionalities here, Removed deprecated functionalities in PHP7
Conclusion:
Personally I believe recent changes of PHP versions, makes PHP as much more matured language. But it is good message to all PHP developers for building awesome enterprise level applications using latest PHP version.

CREDITS


No comments:

Post a Comment