PHP7 will be officially released in October 2015. PHP7 will be a major version update for the PHP script language. It will also bring about significant performance improvements and new features, as well as some outdated features. This release will focus on performance
PHP7 will be officially released in October 2015. PHP7 will be a major version update for the PHP script language. It will also bring about significant performance improvements and new features, as well as some outdated features. This release will focus on performance
So far, PHP has officially released the PHP 7 RC5 version. It is expected that the first official version will be released around May! Now, the major features of PHP 7 must have been finalized and won't be changed. Later versions of iterations mainly involve bug fixing and optimization. Let's talk about the five new features of php7.0 that we have always been expecting.
If you are using a framework based on composer and PSR-4, can this write method successfully load class files? In fact, it is okay. The automatic loading method registered by composer is to find the location based on the namespace of the class when the class is called. This method has no impact on it.
1. Operator (NULL merge operator)
Put this in the first example because I think it is useful. Usage:
$ A = $ _ GET ['a']? 1;
It is equivalent:
<? Php $ a = isset ($ _ GET ['a'])? $ _ GET ['a']: 1;
We know that the ternary operator can be used like this:
$? : 1
However, this is based on the premise that $ a has been defined. New ?? Operators can simplify judgment.
2. function return value type declaration
Examples provided in the official documentation (note... The parameter syntax of the edge length is only available in PHP 5.6 or later versions ):
<? Phpfunction arraysSum (array... $ arrays): array {return array_map (function (array $ array): int {return array_sum ($ array) ;}, $ arrays);} print_r (arraysSum ([1, 2, 3], [, 6], [, 9]);
In this example, we can see that all functions (including anonymous functions) can specify the type of the returned value.
The statement is written in a way similar to swift:
Func sayHello (personName: String)-> String {let greeting = "Hello," + personName + "! "Return greeting}
This feature helps us avoid some problems caused by implicit type conversion in PHP. You can think about the expected results before defining a function to avoid unnecessary errors.
However, there is also a feature that requires attention. PHP 7 adds a declare command: strict_types, which uses both the strict mode.
When using the return value type declaration, if it is not declared as the strict mode, if the return value is not the expected type, PHP will still forcibly convert it to the type. However, if the mode is strict, a Fatal error of TypeError is triggered.
Force mode:
<? Phpfunction foo ($ a): int {return $ a;} foo (1.0 );
The above code can be executed normally. The foo function returns int 1 without any errors.
Strict mode:
<? Phpdeclare (strict_types = 1); function foo ($ a): int {return $ a;} foo (1.0 );
# PHP Fatal error: Uncaught TypeError: Return value of foo () must be of the type integer, float returned in test. php: 6
After the declaration, a fatal error is triggered.
Is it a bit similar to the strict mode of js?
3. scalar type declaration
The parameter type declaration of a function in PHP 7 can be a scalar. In PHP 5, it can only be a class name, interface, array, or callable (PHP 5.4, which can be a function, including an anonymous function ), now you can also use string, int, float, and bool.
Official example:
<? Php // Coercive modefunction sumOfInts (int... $ ints) {return array_sum ($ ints);} var_dump (sumOfInts (2, '3', 4.1 ));
It should be noted that the strict mode mentioned above is also applicable here: the forced type conversion will still be performed for parameters that do not meet expectations in the forced mode (default, both forced type conversion, in strict mode, the critical error of TypeError is triggered.
4. use batch Declaration
In PHP 7, use can declare multiple classes, functions, or const in one 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 };
However, you still need to write the name of each class, function, or const (there is no from some import * method like python ).
It should be noted that if you are using a composer-based and PSR-4-based framework, can this method successfully load class files? In fact, it is okay. The automatic loading method registered by composer is to find the location based on the namespace of the class when the class is called. This method has no effect on it.
5. Other features
I will not introduce other features one by one. If you are interested, please refer to the official documentation:
Briefly speaking:
PHP 5.3 has an anonymous function and now has an anonymous class;
Define can now define a constant array;
A call method is added to the Closure;
The generator (or the iterator is more suitable) can have a final return value (return), or enter another generator (generator delegate) through the new syntax of yield from ).
The two new features of the generator (return and yield from) can be combined. You can test the specific representation on your own. PHP 7 has now reached RC5, and the final version will soon come.
The above is all about the new features of php7.0. I hope you will like this article.