1.?? Operator (NULL merge operator)
Put this on the first one because I think it's very useful. Usage:
$a = $_get[' A ']?? 1;
It is equivalent to:
We know that ternary operators can be used in this way:
$a?: 1
But this is based on the premise that the $a has been defined. What's new?? Operators can simplify judgment.
2. function return value type declaration
An example of an official document (note ...) The edge length parameter syntax is available in PHP version 5.6 or higher):
As you can see from this example, functions (including anonymous functions) can now specify the type of the return value.
The wording of this statement is somewhat similar to Swift:
Func SayHello (personname:string), String {let greeting = "Hello," + PersonName + "!" Return greeting}
This feature can help us avoid some of the problems with the implicit type conversion of PHP. It is possible to avoid unnecessary errors by thinking about the expected results before defining a function.
But there is also a feature to note. PHP 7 Adds a DECLARE directive: Strict_types, which uses strict mode.
When using a return value type declaration, if the return value is not the expected type, then PHP enforces the type conversion if it is not declared as strict mode. However, if it is a strict mode, it will start off with a TypeError Fatal error.
Mandatory mode:
The above code can execute normally, and the Foo function returns int 1 without any errors.
Strict mode:
After the declaration, a fatal error is triggered.
Isn't it a bit similar to JS's strict mode?
3. Scalar type declarations
The formal parameter type declaration of a function in PHP 7 can be a scalar. In PHP 5, only the class name, interface, array, or callable (PHP 5.4, which can be functions, including anonymous functions), can now also use string, int, float, and bool.
Official Example:
It is important to note that the problem of the strict pattern mentioned above is equally applicable here: Forced mode (by default, coercion of type conversions), or coercion of type conversions for non-conforming parameters, which triggers TypeError fatal error in strict mode.
4. Use Batch Declaration
In PHP 7, use can declare multiple classes or functions or const in a sentence:
!--? phpuse Some\namespace\{classa, ClassB, ClassC as C};use function some\namespace\{fn_a, Fn_b, fn _c};use Const Some\namespace\{consta, CONSTB, CONSTC}; But you still have to write down the name of each class or function or const (and no way from some import * like Python). The question that the
needs to be aware of is: if you are using a composer and PSR-4-based framework, will this be a successful load class file?
5. Other features Some of the other features I have not introduced, and are interested to view the official documents:
http://php.net/manual/en/ migration70.new-features.php
Briefly say a few:
-
PHP 5.3 started with anonymous functions, and now has anonymous classes;
-
Define can now define a constant array;
The
-
Closure (Closure) adds a call method;
The
-
Generator (or an iterator that is more appropriate) can have a final return value (return), or it can be entered in a different generator (builder delegate) through the new syntax of yield from. The two new features of the
Generator (return and yield from) can be combined. We can test the concrete appearance. PHP 7 is now up to RC5, and the final version should come soon.
Last small ad:
5pyj5osp57ue5bu65lia5liq5lin5rc055qeuehq55qeuvhnvqtvvjozmdu2mtewodjvvizmrklov47liqdlhaxjgii=