11 August, 2016

New feature of PHP 7

1- 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.

2- 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.

3- Many fatal errors will be consider as exception
try { // Code that may throw an Exception or Error. } catch (Throwable $t) { // Handle exception }
$var = 1; try { $var->method(); // Throws an Error object in PHP 7. } catch (Error $e) { // Handle error }

function add(int $left, int $right) { return $left + $right; } try { $value = add('left', 'right'); } catch (TypeError $e) { echo $e->getMessage(), "\n"; }

try { require 'file-with-parse-error.php'; } catch (ParseError $e) { echo $e->getMessage(), "\n"; }

try { $value = 1 % 0; } catch (DivisionByZeroError $e) {
echo $e->getMessage(), "\n"; } 4- 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.
Example: // syntax for PHP prior 7 $a = isset($b) ? $b : '0'; // Using PHP new operator ?? $a = $b ?? '0'

5 - 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 <=>.
Example: // Integers echo 1 <=> 1; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1 // Floats echo 1.5 <=> 1.5; // 0 echo 1.5 <=> 2.5; // -1 echo 2.5 <=> 1.5; // 1 // Strings echo "a" <=> "a"; // 0 echo "a" <=> "b"; // -1 echo "b" <=> "a"; // 1

6- Scalar Type Declarations
The next interesting feature is the Scalar Type Declarations. Scalar Type Declarations is a variable type declaration for scalar type’s int, float, string, and bool.
1. coercive (flexible) function_scalar(5);// Valid function_scalar('5'); // Valid 2 .STRICT declare(strict_types=1); // Valid function_scalar(5); // Invalid function_scalar('5');

7- 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.

8- 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.
Example: $Obj = new class ( "Mycls" ) { public function __construct ( $arg ) { $this ->arg = $arg; } };

9- 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.

10- error_clear_last () function
Added a new function error_clear_last () for clearing the last occurred error.
Example: 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.

11- 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.
Example: define('MYCONSTANT', array('a','b', 'c')); echo MYCONSTANT[1];

12- 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 later execution. In PHP 5 it was already possible to bind an object to the scope of the closure as if it was a method. In PHP 7 the “call” method has been introduced to simplify the process.
Example: 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;

13- Facilitates Imports From the Same Namespace
The new Group Use Declarations feature will be godsent to those of you who want to import many classes from the same namespace. The new syntax cuts verbosity, makes your code tidier and easier on the eyes, and saves you a lot of typing time.
// Pre PHP 7 code use some\namespace\ClassA; use some\namespace\ClassB; use some\namespace\ClassC as C; use function some\namespace\fn_a; use function some\namespace\fn_b; use function some\namespace\fn_c; use const some\namespace\ConstA; use const some\namespace\ConstB; use const some\namespace\ConstC; // PHP 7+ code use some\namespace\{ClassA, ClassB, ClassC as C}; use function some\namespace\{fn_a, fn_b, fn_c}; use const some\namespace\{ConstA, ConstB, ConstC};

Please read more at: http://blog.teamtreehouse.com/5-new-features-php-7 http://php.net/manual/en/migration70.new-features.php https://blog.feryn.eu/php-7-is-now-available-new-features-improvements/ http://www.hongkiat.com/blog/php7/


Related Posts Plugin for WordPress, Blogger...