php7 new features
1. Space ship operator
Used to compare two expressions, for example, when \(a is less than, equal to or greater than \)b, return -1,0,1 respectively
echo 1 <=> 1; //0 echo PHP_EOL; echo 1 <=> 2; //-1 echo PHP_EOL; echo 2 <=> 1; //1
2. Scalar type and return value type declaration
- php7 can declare parameters of string (string), integer (int), floating point and boolean types.
- The parameter type declaration is not restricted to the default mode and strict mode. In the default mode, when the passed parameter does not conform to the declared type, the type conversion will be tried first (the type conversion here only applies to convertible types. Convert'a' Int will also report an error), while in strict mode, an error will be reported directly
declare(strict_types=1); //strict_types=1 means strict mode function sum(int ...$ints){ return array_sum($ints); } var_dump(sum(1,'2','3.1',4.1));
Results of the
Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given
- If strict mode is turned off, the incoming parameters will be converted to Int first, and the result is 10
Restricted return value type
The following code declares that the return value is int
declare(strict_types=1); function sum(int ...$ints):int { return array_sum($ints); } var_dump(sum(1,2,3)); //6
php7.1 expands the function return value type, you can define the return value type as void, no matter whether the strict mode is turned on, as long as the function has a return statement other than "return;", an error will be reported
declare(strict_types=1); function sum(int ...$ints):void { // return NULL; //Report an error return; // Statements other than "return;" also report errors // return array_sum($ints); } var_dump(sum(1,2,3));
php7.1 has further support for parameter types and return value types. The type can be a null type. Adding "?" before the parameter or return value type declaration means either null or the declared type.
declare(strict_types=1); function test(?int $a):?int { return $a; } var_dump (test (null)); //ZERO var_dump (test (1)); // int (1) var_dump(test('a')); // Fatal error
3.Null merge operator
$page=isset($_GET['p'])?$_GET['p']:0; //php7 $page =$_GET['p'] ??0; //Ternary operator $page =$_GET['p'] ?? $_POST['p'] ??0;
4. Constant array
Before php7, it was impossible to define a constant array through define, php7 supports this operation
define('STUDENT',['boy','girl']); print_r(STUDENT);
5. Namespace batch import
//Before php use Space\ClassA; use Space\ClassB; use Space\ClassC C; //php7 use Space\{ClassA,ClassB,ClassC as C};
6.throwable interface
Before php7, if there is a syntax error or fatal error in the code, the program will directly report an error and exit. The global throwable interface is implemented in php7. The original Exception and some errors implement this interface.
This kind of Error can be caught by the first matching try/catch block like an Exception. If there is no matching catch block, call the exception function to handle it. If the exception handling function has not been registered, it will be processed in the traditional way (Fatal error)
The Error class does not inherit from Exception, so it cannot be caught with catch(Exception $e){}. You can use catch(Error $e){} or register an exception handling function (set_exception_handler()) to catch Error
try{ abc(); }catch (Error $e){ print_r ($ e); } echo 123;
Fatal error in php5, the code after exiting immediately will not be executed
php7
Error Object ( [message:protected] => Call to undefined function abc() [string:Error:private] => [code:protected] => 0 [file:protected] => /mnt/hgfs/www/web/md.php [line:protected] => 4 [trace:Error:private] => Array ( ) [previous:Error:private] => ) 123
Or handle it by registering an exception handling function
set_exception_handler(function($e){ echo "err:".print_r($e,true); }); abc(); echo 123;
Results under php7, php5 still Fatal error
err:Error Object ( [message:protected] => Call to undefined function abc() [string:Error:private] => [code:protected] => 0 [file:protected] => /mnt/hgfs/www/web/md.php [line:protected] => 7 [trace:Error:private] => Array ( ) [previous:Error:private] => ) The rest will not be executed
7.Closure::call()
Before php7, when dynamically adding methods to an object, Closure can be used to copy a closure object and bind it to a $this object and class scope
class People{ private $age=10; } $f=function(){ return $this->age+1; }; $p=$f->bindTo(new People,'People'); echo $p();
In php7, you can temporarily bind a closure object to the $this object and call it through call
class People{ private $age=10; } $f=function(){ return $this->age+1; }; echo $f->call(new People);
8.intdiv
echo intdiv(10,3);
9. List square brackets
$a=[1,2,3]; list($n1,$n2,$n3)=$a; //php7 [$n1,$n2,$n3]=[4,5,6]; //[] is not an array, but a short form of list
10. Foreach traverses the array and no longer modifies the internal pointer
$arr=[1,2,3,4,5,6]; foreach ($arr as $key => $value) { if($value ==2) break; } echo current($arr);//php7 under 1, php5 under 3
11. Anonymous classes can be defined using new class, and anonymous classes can be used instead of complete definitions
Cache interface { public function read (); } class Ca { private $cache; public function setcache(Cache $cache){ $this->cache=$cache; } } $fcache =new Ca; $fcache->setcache(new Class implements Cache { public function read(){ } });
Others, remove ASP and script PHP tags, remove $HTTP_RAW_POST_DATA, anonymous class, constant class visibility, etc.