Five more features of PHP 7

Source: Internet
Author: User
Tags scalar vars

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

    1. <php
    2. function arrayssum (array ... $arrays): array
    3. {
    4. return Array_map (function (array $array): int {
    5. return array_sum ($array);
    6. }, $arrays);
    7. }
    8. 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:

    1. Func SayHello (personname:string), String {
    2. Let greeting = "Hello," + PersonName + "!"
    3. return Greeting
    4. }

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:

    1. <php
    2. function foo ($a): int
    3. {
    4. return $a;
    5. }
    6. Foo (1.0);

The above code can execute normally, and the Foo function returns int 1 without any errors.

Strict mode:

    1. <php
    2. Declare (Strict_types=1);
    3. function foo ($a): int
    4. {
    5. return $a;
    6. }
    7. Foo (1.0);
    8. # 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:

    1. <php
    2. Coercive mode
    3. function sumofints (int ... $ints)
    4. {
    5. return array_sum ($ints);
    6. }
    7. 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:

    1. <php
    2. Use Some/namespace/{classa, ClassB, ClassC as C};
    3. Use function some/namespace/{fn_a, Fn_b, fn_c};
    4. 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

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.