Precautions from PHP5 to PHP7 (2) some new features of PHP7

Source: Internet
Author: User
Precautions from PHP5 to PHP7 (2) some new features of PHP7

1. define defined constants not only support scalar, but also support arrays


Define ('phpxy', array (
'Fengjie ',
'Sister Furong ',
'Yang mi is dump'
));

Echo PHPXY [1]; // The output is "Sister Furong"
?>


2. supported comparison <=> strings, arrays, and integers.


// Integers
Echo 1 <=> 1; // 0
Echo 1 <=> 2; // 1
Echo 2 <=> 1; // 1

PHP school evaluation: We believe that the actual user base is small and the usage in actual work is not very high.


3. in PHP7, the mandatory parameter type of the function is supported.


// Coercive mode
Function sumOfInts (int... $ ints)
{
Return array_sum ($ ints );
}

Var_dump (sumOfInts (2, '3', 4.1 ));


// 9 is output in this example. why? -- This is mandatory for integer type.


PHP evaluation: function funcName (... parameter) is supported by php5.6. Do not think it is a knowledge point in PHP 7



4. the return type is also mandatory


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



5. the call method is created to be called externally. in a shorter way, the anonymous function is appended to the peer and the call is completed.

Class A {private $ x = 1 ;}

// Pre PHP 7 code
$ GetXCB = function () {return $ this> x ;};
$ GetX = $ getXCB> bindTo (new A, 'A'); // intermediate closure
Echo $ getX ();

// PHP 7 + code
$ GetX = function () {return $ this> x ;};
Echo $ getX> call (new );


6. the member method can also specify that a class type must be returned.


Class {}
Class B extends {}

Class C
{
Public function test ():
{
Return new;
}
}

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.