PHP7 new scalar, operator, return value type features detailed

Source: Internet
Author: User
Tags scalar
1.?? operator (NULL merge operator)

$a = $_get[' A ']?? 1;

It is equivalent to:

$a = empty ($_get[' a '])? 1: $_get[' a '];

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. Simplify the code and also more intuitive!

2. function return value type declaration

Official documentation provides examples (note that ... the edge length parameter syntax is only available in PHP 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.

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 use 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 TypeError with a 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);

After the declaration, a fatal error is triggered.

# PHP Fatal Error:  uncaught typeerror:return value of foo () must is of the type integer, float returned in Test.php:6

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 be used string , int float and bool .

<?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 also applicable here: Coercion mode (by default, coercion of type conversions) or coercion of type conversions on non-conforming parameters, TypeError fatal errors triggered 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\na Mespace\{consta, Constb, CONSTC};

However, the name of each class or function or const (and no Python-like method) is still to be written from some import * .

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.

Let's say a few simple words:

1, PHP 5.3 began to have an anonymous function, now has an anonymous class;

2, define can now define a constant array;

3, Closure (Closure) added a call method;

4, the generator (or the iterator is more appropriate) can have a final return value (return), you can also pass yield from the new syntax into a different generator (generator delegate).

The two new features (return and) of the generator yield from can be combined. Specific people can test themselves.

Related Article

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.