If you are using a framework based on composer and PSR-4, will this be a successful loading of class files? In fact, the automatic loading method of composer registration is to find the location according to the namespace of the class when the class is called, which has no effect on it.
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:
<php
$a = isset ($_get[' a ')? $_get[' A ']: 1;
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):
- <php
- function arrayssum (array ... $arrays): array
- {
- return Array_map (function (array $array): int {
- return array_sum ($array);
- }, $arrays);
- }
- Print_r ([Arrayssum], [4,5,6], [7,8,9]);
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:
- <php
- function foo ($a): int
- {
- return $a;
- }
- Foo (1.0);
The above code can execute normally, and the Foo function returns int 1 without any errors.
Strict mode:
- <php
- Declare (Strict_types=1);
- function foo ($a): int
- {
- return $a;
- }
- Foo (1.0);
- # PHP Fatal error:uncaught typeerror:return value of foo () must be's of the type integer, float returned in Test.php:6
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:
- <php
- Coercive mode
- function sumofints (int ... $ints)
- {
- return array_sum ($ints);
- }
- Var_dump (Sumofints (2, ' 3 ', 4.1));
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:
- <php
- Use Some/namespace/{classa, ClassB, ClassC as C};
- Use function some/namespace/{fn_a, Fn_b, fn_c};
- Use const Some/namespace/{consta, CONSTB, CONSTC};
However, the name of each class or function or const is still written (and there is no way from some import * like Python).
The question to keep in mind is: if you're using a framework based on composer and PSR-4, does this kind of writing succeed in loading class files? In fact, the automatic loading method of composer registration is to find the location according to the namespace of the class when the class is called, which has no effect on it.
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 begins with an anonymous function, and now it has an anonymous class;
Define can now define a constant array;
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 (generator 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.
Five more features of PHP 7