Home > Article > Backend Development > New features we should learn to use in PHP7
PHP7 brings significant performance improvements and new features, as well as improvements to some features of previous versions. This article will work with you to understand and discuss the new features in PHP7.
1. Scalar type declaration
We know that PHP is a weakly typed programming language, so it does not provide any method to specify the type of input parameters and return values. PHP7 breaks this status quo and adds support for scalars Type (int, float, string, bool) declaration support, add declare(strict_types=1) instruction to declare whether strict type checking, let’s look at a piece of code:
declare(strict_types=1) function add(int $x, int $y) : int { return $x + $y; } echo add(1, 2); //int(7) declare(strict_types=1) function add(int $x, int $y) : int { return $x + $y; } echo add(1, 2); //int(7)
Valid types are: class/interface name, self, array, callable, bool, float, int and string.
2. NULL merging operator
The NULL merging operator has been added to PHP7. Don’t underestimate this “??”, it will be very convenient for us with it. Gets a parameter and can provide a default value if it is empty. How does the ?? operator return the left side if the value on its left side exists and is not NULL, otherwise its right side value will be returned. Let’s experience the power of the ?? operator through the following piece of code.
<?php // 获取user参数的值(如果为空,则用'nobody') // PHP5中我们这样来实现: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; // PHP7中,使用??运算符更便捷: $username = $_GET['user'] ?? 'nobody'; ??> <?php // 获取user参数的值(如果为空,则用'nobody') // PHP5中我们这样来实现: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; // PHP7中,使用??运算符更便捷: $username = $_GET['user'] ?? 'nobody'; ??>
3. Anonymous class
As the name suggests, there is no class name, and its declaration and instantiation are at the same time. PHP7 supports instantiating an anonymous class through new class. You can Used to replace some "burn after use" complete class definitions.
echo (new class() { public function myMethod() { return "Hello!"; } })->myMethod(); //Result: Hello! echo (new class() { public function myMethod() { return "Hello!"; } })->myMethod(); //Result: Hello!
4. More Errors can be handled with exceptions
More Errors in PHP7 become catchable Exceptions and are returned to the developer. If they are not caught, It is an Error. If caught, it becomes an Exception that can be handled within the program. By default, Error will directly cause the program to interrupt, while PHP7 captures and handles it through try/catch blocks, allowing the program to continue executing, providing programmers with more flexible options.
Code example:
nonExistFunction($arg); // It will generate fatal error nonExistFunction($arg); // It will generate fatal error
At this time, the above code will prompt the error "Fatal error: Call to a member function method() on a non-object", and this fatal error will stop the following Code execution continues.
So if you want to continue executing the code, you can solve it through exception handling:
try { nonExistFunction($arg); //This method is not exists then it will be go to catch } catch (EngineException $e ) { echo "Exception: {$e->getMessage()}n"; } try { nonExistFunction($arg); //This method is not exists then it will be go to catch } catch (EngineException $e ) { echo "Exception: {$e->getMessage()}n"; }
5. Combined with the comparison operator 96b4fef55684b9312718d5de63fb7121
This doesn’t require much explanation. Let’s look directly at the sample code. You can easily understand the function of this operator through the code.
// PHP 7之前的写法:比较两个数的大小 function func ( $ a , $ b ) { return ($a < $b) ? -1 : (($a > $b) ? 1 : 0) } // PHP新增的操作符 <=> function func ( $ a , $ b ) { return $a <=> $b; } // PHP 7之前的写法:比较两个数的大小 function func ( $ a , $ b ) { return ($a < $b) ? -1 : (($a > $b) ? 1 : 0) } // PHP新增的操作符 <=> function func ( $ a , $ b ) { return $a <=> $b; }
6. Define array constants
In the past, when we used define() to define constants, the data type only supported scalar, but in PHP7, it supports defining array types. constant.
define('MYCONSTANT', array('a','b','c')); define('MYCONSTANT', array('a','b','c'));
PHP7 has many new features. Today we will introduce these first. We will continue to update them in the future. We also welcome additions from PHPers. Let’s communicate, learn and make progress together.