In this article, We will discuss about the new features of PHP7.PHP7 will be officially released on November 2015. PHP7 will be a major update for PHP scripting language and it will bring significant performance improvements with new features.It will have improvement in some outdated features too. The release version will mainly focus on performance optimization to speed up execution of data.
Here I am going to explain some new features of PHP7
- Improved performance
- Consistent 64-bit support
- Many fatal errors will be consider as exception
- Removal of old and unsupported SAPIs and extensions
- The null coalescing operator (??)
- Combined comparison Operator (<=>)
- Scalar Type Declarations
- Return Type Declarations
- Anonymous Class Support
- UVS (Unified variable syntax)
- AST (Abstract Syntax Tree)
- error_clear_last () function
- Array in define () function
- Improved performance :
One of the major changes in PHP7 is a performance improvement. The performance improvement is quite an interesting feature because it is much faster and uses significantly less memory. so it will definitely bear much more traffic now without any additional costs.
- Consistent 64-bit support :
PHP 7 with full 64-bit support which is very impressive.It reduces the occupied size and optimize data structures.A small drawback new implementation – slightly excessive memory allocation.
- Many fatal errors will be consider as exception :
In New PHP 7, fatal errors will be significant as the exception so you can catch fatal errors and handle them in the future. Using this feature, fatal errors will be handled by a try / catch block and you will be able to continue to execute code without problems.
Example:
Currently, above code will generate error “Fatal error: Call to a member function method() on a non-object ” and a fatal error will stop the execution of next code too.
So, If you want to, you can continue to catch fatal error with exception:
1 2 3 4 5 6 7 | try { nonExistFunction($arg); //This method is not exists then it will be go to catch } catch (EngineException $e ) { echo "Exception: {$e->getMessage()}\n"; } |
- Removal of old and unsupported SAPIs and extensions :
Unfortunately, Numerous Server API(apache, ISAPI extensions) which no longer supported.
- The null coalescing operator (??) :
The Coalesce operator will be the very useful feature added into PHP7, we will certainly get used it very much easily.
Let’s understand it by checking old and new method:
1 2 3 4 5 6 |
The null coalescing operator will return value to his left if different than null while another case is the value is on the right side of the operator.
- Combined comparison Operator (<=>) :
This is some cool feature of PHP7.It is also known as spaceship operator.The new comparison Operator is looks like this <=>.
Here is a quick look at this feature with example:
1 2 3 4 5 6 7 8 9 10 | // syntax for PHP prior 7 : To Compare the size of two numbers function func ( $ a , $ b ) { return ($a < $b) ? -1 : (($a > $b) ? 1 : 0) } // Using PHP new operator <=> function func ( $ a , $ b ) { return $a <=> $b; } |
As per above code, it will return -1 if the left side is larger than the right, return 0 if they are the same and return 1 if the right side is larger than the left. Isn’t it simple to use new operator?
- Scalar Type Declarations :
The next interesting feature is the Scalar Type Declarations. Scalar Type Declarations is a variable type declaration for scalar types int, float, string, and bool.
1 2 3 4 5 6 7 8 9 10 | function function_scalar (int $myinteger) { var_dump ( $myinteger ); } function_scalar(5); //Catchable fatal error: Argument 1 passed to function_scalar() must be an instance of int, integer given function_scalar('5'); |
In both cases, Earlier PHP generates an error “Catchable fatal error: Argument 1 passed to function_scalar() must be an instance of int”.So you are not allowed to pass data type before the parameter into the function. However, It is newly added in PHP7.
Scalar Type Declarations is support two types:
1. coercive (flexible) :
This is default type and, in this case, $myinteger variable will receive the parameter type will be an integer for function, PHP will do an automatic conversion of the given value
1 2 3 4 | function_scalar(5);// Valid function_scalar('5'); // Valid |
2. STRICT :
STRICT type means if you use integer variable, it would contain integer value of variable and if float type in parameter, would store decimal values and so on.
SYNTAX To enable strict mode:
1 2 3 | declare (strict_types = 1); |
This must be declared at the first line in the PHP files.when strict_types set to 0, PHP would not strictly check parameters and return value type.
So, with the STRICT Type would be allowed only integer type.
1 2 3 4 5 6 7 | declare(strict_types=1); // Valid function_scalar(5); // Invalid function_scalar('5'); |
- Return Type Declarations :
Return Type Declarations should allow to indicate the return type of a function such as int, float, string and bool, array, callable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | declare(strict_types=1); // function that returns an integer function return_func($a ,$b ) { return $a + $b ; } // function that return a string instead of an integer function return_func(int $a , int $b ) :int { return strval ( $a + $b ); } var_dump(return_func(4,5)); |
- Anonymous Class Support :
PHP 7 introduced anonymous class which is previously supported by the most advanced language JAVA. Anonymous Class means declaration and the instance of the class take place in the same time.Also, it is not necessary to assign a name to it.
Let’s see the example to define and instantiate a class simultaneously:
1 2 3 4 5 6 7 | $Obj = new class ( "Mycls" ) { public function __construct ( $arg ) { $this ->arg = $arg; } }; |
- UVS (Unified variable syntax) :
The unified variable syntax will allow you to write some nice lines of code for which generally needed for temporary variables.From now onwards, you will be able to call the function assigned to the class attributes and also combine variables / static methods.
Some Examples:
1 2 3 4 | $Obj()['bar'] () getstr() {0} |
- AST (Abstract Syntax Tree) :
Another wonderful feature of PHP 7 is the Abstract Syntax Tree (AST). Earlier,The PHP parser generates directly OpCodes whereas In PHP 7, the parser will work as an intermediate structure to generate the AST and parser will be decoupling compiler to reduce the amount of code and make it easier to understand and maintain easily.
- error_clear_last () function :
Added a new function error_clear_last () for clearing the last occurred error.
Example:
1 2 3 | var_dump(error_get_last()); |
The error_get_last() function will improve the way to handle errors and you can easily get your last error in PHP.
- Array in define () function :
To assign an array values in define() function for constant was not possible before.It is possible to use array value in PHP7.
Let’s illustrate this with an example:
1 2 3 | define('MYCONSTANT', array('a','b', 'c')); |
Earlier PHP7, we can not assign array value to constant so it would be very helpful features for us.
The php7 features list is quite long.Certainly there are other more features too but not study in-depth so that’s it. I am really excited about the future of PHP 7. Do we really need those changes? Let me know if you like any of the new features in PHP 7.
Comments (3)