Home >php教程 >php手册 >PHP7 new features introduction

PHP7 new features introduction

WBOY
WBOYOriginal
2016-08-20 08:47:351494browse

We have compiled some commonly used new features, welcome to like! ! !

New operator
1. ??
$username = $_GET['user'] ?? '';
$username = isset($_GET['user']) ? $_GET['user'] : ' nobody';

2. <=>
$number1 <=> $number2; When $number1 is less than, equal to, or greater than $number2, return -1,0,1 respectively


New function
intdiv(divisor, divisor) — round the division result
intdiv(3, 2) //1



define can define arrays
define('ANIMALS', [
'dog',
'cat',
'bird'
]);


Return type declaration
function test() :int
{
return 1; //true
return '1'; //true
return 'string'; //false
}


Scalar type declaration
function test(string $name) :int
{
return 22;
}
string integer float boolean


Can catch core errors
Error hierarchy
Throwable
 Error
 ArithmeticError
  DivisionByZeroError

  AssertionError
  ParseError
  TypeError

Exception


Optimization of core sorting
php5: array(1=>0, 0=> 0) //Quick sort (unstable sort)
php7: array(0=>0, 1=>0) //Quick sort + selection sort (stable sort)



More abstract syntax than php5 Number (abstract snytax tree) AST
PHP -> Parser -> AST ->Opcodes -> Execution
Performance has increased, memory consumption has also increased but is negligible


Anonymous classes can be instantiated through new class An anonymous class
function getAnonymousClass($config) {
return new class($config) {};
}



Temporarily bind a method to the object and call
$f = function() {
p($ this->name);
};

class F {
private $name = 'F';
}

$f->call(new F);




Unified syntax variables
Brackets do not affect behavior from left to right

$a = 'b';
$b = ['1', '2 ', '3'];
var_dump($$a[1]);
var_dump(($$a)[1]);

php5:
Notice: Uninitialized string offset: 1 in E:Program FilesphpStudyWWWtest.php on line 4
Notice: Undefined variable: in E:Program FilesphpStudyWWWtest.php on line 4
NULL

Parse error: syntax error, unexpected '[' in E:Program FilesphpStudyWWWtest.php on line 4

php7: string(1 ) "2" string(1) "2"


Expression PHP5 PHP7 $$foo['bar']['baz'] ${$foo['bar']['baz']} ($$foo )['bar']['baz']
$foo->$bar['baz']   $foo->{$bar['baz']}   ($foo->$bar)['baz ']
$foo->$bar['baz']()   $foo->{$bar['baz']}()  ($foo->$bar)['baz']()
Foo::$bar['baz']()   Foo::{$bar['baz']}()   (Foo::$bar)['baz']()




About namespaces
/ / Pre PHP 7 code
use somenamespaceClassA;
use somenamespaceClassB;
use somenamespaceClassC as C;

use function somenamespacefn_a;

use function somenamespacefn_b;
use function somenamespacefn_c;

use const somenamespaceConstA;

use const somenamespaceConstB;
use const somenamespaceConstC;

// PHP 7+ code

use somenamespace{ClassA, ClassB, ClassC as C};
use function somenamespace{fn_a, fn_b, fn_c};
use const somenamespace{ConstA, ConstB, ConstC};


modification of list
1.
list($array[], $array[], $array[]) = [1, 2, 3];
var_dump($array);
php5: array(3) { [0]=> ; int(3) [1]=> int(2) [2]=> int(1) }
  php7: array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }


2. Assigning null value is not allowed
list() = $a;
list(,,) = $a;
list($x, list (), $y) = $a;

php7 will report Fatal error: Cannot use empty list

3. String splitting is no longer supported
$string = "xy";
list($x, $y) = $string;
var_dump($x, $y);

php5: string(1) "x" string(1) "y"
php7: null null

Modification of foreach
Variable reference will affect the array Loop of
$array = [0];
foreach ($array as &$val) {
var_dump($val);
$array[1] = 1;
}
php5: int(0)
php7: int (0) int(1)

Reference source address: http://www.php7.site/book/php7/variable-changes-22.html

Tips to improve PHP performance: http://www.laruence.com/2015/12/04/3086.html

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn