Five features of PHP 7

Source: Internet
Author: User
Tags anonymous define array final functions generator new features string

1. Operator (NULL merge operator)

Put this in the first place 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 $a have already defined. What's new?? operator can simplify judgment.

2. function return value type declaration

Examples provided by official documents (note ...) The side-length parameter syntax is available in PHP more than 5.6 versions:

 
  
  
  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 ([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 return value.

The wording of this statement is somewhat similar to that of 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 implicit type conversions in PHP. You can avoid unnecessary errors by thinking about the desired results before you define a function.

But there is also a feature that needs to be noted. PHP 7 Adds a declare directive: Strict_types, which uses strict mode.

When using a return value type declaration, if it is not declared as strict mode, PHP will cast the value if it is not the expected type. But if the strict mode, then will set off a typeerror Fatal error.

Force mode:

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

The code above can be executed correctly, 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 is of the type integer, float returned in Test.php:6

After the declaration, a fatal error is triggered.

Is it somewhat similar to the strict mode with JS?

3. Scalar type declaration

The parameter type declaration of a function in PHP 7 can be a scalar. You can use string, int, float, and bool in PHP 5 only if it is a class name, interface, array, or callable (PHP 5.4, which can be functions, including anonymous functions).

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 also useful to note that the problem of the strict pattern mentioned above is equally applicable here: Forced mode (by default, coercion type conversion) or coercion type conversion for parameters that do not conform to the expected, and in strict mode triggers typeerror fatal errors.

4. Use Volume 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};

But you still have to write out the name of each class or function or const (and no method like Python from some import *).

The question to be aware of is: if you are using a framework based on composer and PSR-4, does this writing successfully load class files? In fact, it is possible, composer registration of automatic loading method is called in the class according to the namespace of the class to find the location, this style has no effect on it.

5. Other Features

Some of the other features I will not be introduced, interested in viewing the official documents: http://php.net/manual/en/migration70.new-features.php

Briefly say a few:

    • PHP 5.3 starts with an anonymous function and now has an anonymous class;

    • Define can now define a constant array;

    • The closure (Closure) adds a call method;

    • The generator (or iterator) can have a final return value (returns), or it can be entered into a different generator (the generator delegate) through the new syntax of yield from.

The two new features of the generator (return and yield from) can be combined. Concrete appearances can be tested by ourselves. PHP 7 is now up to RC5, and the final version should come soon.



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.