[PHP series] Detailed description of new features of PHP 7.0, and 7.0 details

Source: Internet
Author: User

[PHP series] Detailed description of new features of PHP 7.0, and 7.0 details

Let's start to introduce the new features of PHP7.0. For details, refer to the introduction on the official website.

Http://php.net/manual/en/migration70.new-features.php1 .?? Operator (NULL merge operator)
$a = $_GET['a'] ?? 1;

It is equivalent:

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

We know that the ternary operator can be used like this:

$a ?: 1

However, this is based on the premise that $ a has been defined. New ?? Operators can simplify judgment. Simplified code and more intuitive!

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], [4,5,6], [7,8,9]));

In this example, we can see that all functions (including anonymous functions) can specify the type of the returned value.

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 addsDeclareCommand:strict_types, Both use 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 it is in strict mode,TypeErrorFatal error.

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

After the declaration, a fatal error is triggered.

# PHP Fatal error:  Uncaught TypeError: Return value of foo() must be of the type integer, float returned in test.php:6
3. scalar type declaration

The parameter type declaration of a function in PHP 7 can be a scalar. In PHP 5, only class names, interfaces,arrayOrcallable(PHP 5.4 can be a function, including an anonymous function ).string,int,floatAndbool.

<?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, triggered in strict ModeTypeError.

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 (not the same as python ).from some import *).

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.

Let's talk about the following:

1. PHP 5.3 has an anonymous function and now has an anonymous class;

2. define can now define a constant array;

3. A call method is added to the Closure;

4. The generator (or the iterator is more suitable) can have a final return value (return), oryield fromInto another generator (generator delegate ).

Two new features of the generator (return andyield from) Can be combined. You can test it on your own.

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.