Porting from PHP 5.6.x to PHP 7.0.x
New features
http://php.net/manual/zh/migration70.new-features.php
new Feature ¶ scalar type declaration ¶
There are two modes of scalar type declarations: coercion (default) and strict mode. You can now use the following type parameters, whether in mandatory or strict mode: string, Integer (int), floating-point number (float), and Boolean value (BOOL). They extend the other types introduced in PHP5: class names, interfaces, arrays, and callback types. <?php
Coercive mode
function sumofints (int ... $ints)
{
Return Array_sum ($ints);
}
Var_dump (sumofints (2, ' 3 ', 4.1));
The above routines will output:
Int (9)
To use strict mode, a declare declaration instruction must be placed at the top of the file. This means that strictly declaring a scalar is based on file availability. This directive affects not only the type declaration of the parameter, but also the return value declaration of the function (see return value type declaration, built-in PHP function, and PHP function loaded in the extension)
Complete scalar type declaration documents and examples see type declaration chapters. return value type declaration ¶
PHP 7 adds support for the return type declaration. Similar to a parameter type declaration, a return type declaration indicates the type of the function return value. The available types are the same as those available in the parameter declaration. <?php
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]);
The above routines will output:
Array
(
[0] => 6
[1] =>
[2] =>
)
Complete scalar type declaration documents and examples can see return value type declarations. null merge operator ¶
Because of the large number of simultaneous use of ternary expressions and isset () in daily use, we have added the syntactic sugar of the null merge operator (??). If a variable exists and the value is not NULL, it returns its own value, otherwise it returns its second operand. <?php
// fetches the value of $_get[' user '] and returns ' nobody '
// if it does not exist.
$username = $_get[' user '] ?? ' nobody ';
// this is equivalent to:
$username = isset ($_get[' user ']) ? $_ get[' user '] : ' nobody ';
// coalesces can be chained: this will return the first
// defined value out of $_get[' user '], $_post[' user '], and
// ' Nobody '.
$username = $_get[' user '] ?? $_post[' user '] ?? ' nobody ';
?> spacecraft operator (combination comparison) ¶
The
Spacecraft operator is used to compare two expressions. When $a is less than, equal to, or greater than $b, it returns 1, 0, or 1, respectively. The principle of comparison is to follow the regular comparison rules of PHP. <?php
// 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
?> defines a constant array through define () ¶
Constants of Array types can now be defined by Definedefine (). Only the const definition can be used in PHP5.6. <?php
Define (' ANIMALS ', [
' Dog ',
' Cat ',
' Bird '
]);
Echo Animals[1]; Outputs "Cat"
?> Anonymous class ¶
It now supports instantiating an anonymous class with the new class, which can be used to replace the full class definition of "burn after". <?php
Interface Logger {
Public function log (string $msg);
}
Class Application {
Private $logger;
Public Function GetLogger (): Logger {
return $this->logger;
}
Public Function Setlogger (Logger $logger) {
$this->logger = $logger;
}
}
$app = new Application;
$app->setlogger (new class implements Logger {
Public function log (string $msg) {
Echo $msg;
}
});
Var_dump ($app->getlogger ());
?>
The above routines will output:
Object (class@anonymous) #2 (0) {
}
Detailed documentation can refer to anonymous classes. Unicode codepoint translation Syntax ¶
This accepts a Unicode codepoint in the form of 16 and prints a string of double quotes or Heredoc enclosed UTF-8 encoded formats. Any valid codepoint can be accepted, and 0 of the beginning can be omitted. echo "\u{aa}";
echo "\U{0000AA}";
echo "\u{9999}";
The above routines will output:
ª
ª (same as before but with optional leading 0 ' s)
incense
closure::call () ¶
Closure::call () now has better performance, a short and able temporary binding method to the object closure and call it. <?php
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 A);
The above routines will output:
1
1
provides filtering for unserialize () ¶
This feature is designed to provide a more secure way to unpack unreliable data. It prevents potential code injection by means of a whitelist. <?php
// converts all objects into __php_incomplete_class object
$ Data = unserialize ($foo, ["allowed_classes" => false]);
// converts all objects into __php_incomplete_class object except those of myclass and myclass2
$data = unserialize ($foo, [) allowed_ Classes "&NBSP;<