An in-depth analysis of the new features of PHP7.0 (five features) _php example

Source: Internet
Author: User
Tags closure generator scalar

So far, the official PHP has released the RC5 version of PHP7, is expected to release the first official version around November! For now, PHP7 's major features must have been stereotyped, and there will be no further changes. Later iterations are mainly about fixing bugs, optimizations, and the like. Here's what we've been looking forward to php7.0 five features.

If you are using a framework based on composer and PSR-4, does this writing succeed in loading 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.

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:

<?php
function 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]);

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:

Func SayHello (personname:string)-> String {let
  greeting = "Hello," + PersonName + "!"
  Return greeting
}

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:

<?php
function foo ($a): int
{return
  $a;
}
Foo (1.0);

The code above can be executed correctly, and the Foo function returns int 1 without any errors.

Strict mode:

<?php
Declare (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 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:

<?php
//Coercive mode
function sumofints (int ... $ints) {return
  array_sum ($ints);
}
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:

<?php use
Some/namespace/{classa, CLASSB, classc as C};
Use function some/namespace/{fn_a, Fn_b, fn_c};
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.

The above is about php7.0 new features of the entire content, I hope this article to introduce everyone like.

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.