Precautions from PHP5 to PHP7 (2) some new features of PHP7
1. define defined constants not only support scalar, but also support arrays
Define ('phpxy', array (
'Fengjie ',
'Sister Furong ',
'Yang mi is dump'
));
Echo PHPXY [1]; // The output is "Sister Furong"
?>
2. supported comparison <=> strings, arrays, and integers.
// Integers
Echo 1 <=> 1; // 0
Echo 1 <=> 2; // 1
Echo 2 <=> 1; // 1
PHP school evaluation: We believe that the actual user base is small and the usage in actual work is not very high.
3. in PHP7, the mandatory parameter type of the function is supported.
// Coercive mode
Function sumOfInts (int... $ ints)
{
Return array_sum ($ ints );
}
Var_dump (sumOfInts (2, '3', 4.1 ));
// 9 is output in this example. why? -- This is mandatory for integer type.
PHP evaluation: function funcName (... parameter) is supported by php5.6. Do not think it is a knowledge point in PHP 7
4. the return type is also mandatory
Function arraysSum (array... $ arrays): array
{
Return array_map (function (array $ array): int {
Return array_sum ($ array );
}, $ Arrays );
}
Print_r (arraysSum ([1, 2, 3], [4, 5, 6], [7, 8, 9]);
5. the call method is created to be called externally. in a shorter way, the anonymous function is appended to the peer and the call is completed.
Class A {private $ x = 1 ;}
// Pre PHP 7 code
$ GetXCB = function () {return $ this> x ;};
$ GetX = $ getXCB> bindTo (new A, 'A'); // intermediate closure
Echo $ getX ();
// PHP 7 + code
$ GetX = function () {return $ this> x ;};
Echo $ getX> call (new );
6. the member method can also specify that a class type must be returned.
Class {}
Class B extends {}
Class C
{
Public function test ():
{
Return new;
}
}