Several new features of PHP 7

Source: Internet
Author: User
Tags scalar

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):

<?phpfunction 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:

<?phpfunction 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:

<? Phpdeclare (Strict_types=1), function foo ($a): int{    return $a;} Foo (1.0); PHP Fatal error:  uncaught typeerror:return value of foo () must is of the type integer, float returned in t Est.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 modefunction 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:

<? Phpuse Some\namespace\{classa, ClassB, ClassC as C};use function some\namespace\{fn_a, Fn_b, Fn_c};use const SOME\NAM Espace\{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, 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.

Several new features of PHP 7

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.