PHP7 new Features

Source: Internet
Author: User

1. Operator (NULL merge operator)
Put this on the first one because I think it's very useful. Usage:
[PHP] Plain Text view Copy code ?
1 $a= $_GET[‘a‘] ?? 1;
It is equivalent to:
[PHP] Plain Text view Copy code ?
12 <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.the new?? operator can simplify the 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] Plain Text view Copy code ?
123456789 <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]));

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:
[PHP] Plain Text view Copy code ?
1234 func sayHello(personName: String) -> String {     let greeting = "Hello, "+ personName + "!"    returngreeting }

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] Plain Text view Copy code ?
1234567 <php functionfoo($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] Plain Text view Copy code ?
1234567 <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 [PHP] Plain Text view Copy code ?
1234567 <php //coercive mode function sumofints (int ... $ints ) {       return array_sum ( $ints } var_dump (sumofints (2, " 3 '

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.
[PHP] Plain Text view Copy code ?
1 4. use批量声明

In PHP 7, use can declare multiple classes or functions or const in a sentence:
[PHP] Plain Text view Copy code ?
1234 <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.

PHP7 new Features

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.