PHP7 is coming, the internet also spread a lot of scattered related news, PHP7 in the end what are the changes? Our phper will need to be changed again, and this blog will try to enumerate.
PHP7 is not compatible with the old place.
Because of the adoption of the AST (Abstract Syntax tree) and the uniform Variable Syntax, some of the code's meaning changes, such as:
$obj-> $properties [' name ']
PHP5.6 is equivalent to
$obj->{$properties [' Name ']}
But in PHP7 equivalent to
{$obj-> $properties} [' Name ']
To avoid problems when upgrading PHP7 in the future, you can now write the code more clearly by using curly braces.
Performance
Due to the adoption of phpng, performance will be promoted, this should be the topic of the most PHP7 chat, of course, this to end users only by upgrading PHP can improve performance without pain, is certainly the most interesting. In addition to performance improvements, memory usage can be improved as the structure of the data store is further optimized.
Low version compatibility
Some of the compatible code will be canceled, such as the previous ASP-style start tag and the <script language= "PHP" > Start tag.
There is also a large wave of features or extensions that are about to be removed, such as Ext-mysql.
One interesting little change is that multiple default statements (what!) will not be allowed in the switch. Was it possible before? )。
New features (there should be applause here)
The urine point has passed, the wonderful part has arrived.
Type restrictions for normal variables and return types
In other words, whether you want to add a type limit to a generic variable (that is, a type such as C that needs to be restricted to a generic type parameter, such as Int,float) is almost unanimously passed by the PHP community, but the proposed author leaves the PHP development team and revokes the RfC ... As a result, there are more than one RFC on the type limit, and each of them is pinched for the sake of the concrete implementation. Finally, however, the original proposal passed the final decision.
Well, for our end users, we can now add a type limit to the generic variable. However, the default type limit is not strict, what does it mean, such as function foo (int $bar) {}, if the first argument is passed the string "123", it is passed the number 123, and then passed to the variable $bar.
However, we can change the default behavior by adding code declare (Strict_types=1) at the top of the file, at which point the Fatal Error is generated if the first argument passed in by the function is a string.
In addition, PHP7 also supports defining return types, and behavior is similar to parameters:
function IsEven (int $number): bool
{
return $number% 2 = 1;
}
Combined contrast operator
Before you compare two numbers to whether a is greater than B, or a is less than B, or a equals B, you must write at least two judgments:
($a < $b)? -1: (($a > $b)? 1:0)
With the combined contrast operator, the code above is equivalent to
$a <=> $b
In the PHP kernel code The,<=> operator is called "T_spaceship" (Space ship) ... Called the space ship operator or the spacecraft operator is still very domineering have wood.
Unicode Escape syntax
You can use \u{xxxx} in a string to represent a Unicode character.
?? Operator
Similar to: operator, but equivalent to Isset ($foo)? $foo: False
Returns a $foo if the $foo is not NULL, otherwise returns ' default '
$foo?? ' Default '
// ?? can be called continuously
$config = $config?? $this->config?? Static:: $defaultConfig;
anonymous function bindings
PHP5.4 can already bind a method to an object and make it simpler in PHP7:
Class HelloWorld
{
Private $greeting = "Hello";
}
$closure = function ($whom) {echo $this->greeting. ' ' . $whom; }
$obj = new HelloWorld ();
$closure->call ($obj, ' world '); Hello World
Grouping with use statements
The USE statement can only be written before PHP7.
Use Symfony\component\httpfoundation\request;
Use Symfony\component\httpfoundation\response;
PHP7 can write that.
Use symfony\componenet\httpfoundation {
Request;
Response;
}
Improved version Builder
The generator cannot have a return value before PHP7, otherwise it will be an error. However, there is no problem in the PHP7, only need to call the Getreturn () method.
Function Gen () {
Yield "Hello";
Yield "";
Yield "world!";
Return "Goodbye moon!";
}
$gen = Gen ();
foreach ($gen as $value) {
Echo $value;
}
Outputs "Hello" on Iteration 1, "" to iterator 2, and "world!" on Iteration 3
echo $gen->getreturn (); Goodbye moon!
Builder delegate
PHP7 also supports mutual delegation of generators, such as:
function Hello () {
Yield "Hello";
Yield "";
Yield "world!";
Yield from Goodbye ();
}
function Goodbye () {
Yield "Goodbye";
Yield "";
Yield "moon!";
}
$gen = Hello ();
foreach ($gen as $value) {
Echo $value;
}
The above code in turn outputs "Hello" "" world! "in each iteration. "Goodbye" "moon!", of course, entrusted to themselves is also possible, but must be careful not to die cycle.
More types of Exception
In PHP7, internal errors are also thrown in a Exception manner. Of course he is a separate type, called \engineexception, and later many errors will be replaced by Engineexception.
In addition, the Code in eval () throws a \parseexception if an error occurs, and throws a \typeexception if the function's argument type finds no match for the pass parameter. If you are dealing with traditional Exception and engineexception at the same time, you have to capture the base class that they share \baseexception
Well, PHP7 's new function is almost all that, look forward to the arrival of PHP7.