Analyze PHP 5.5 new features _php tips

Source: Internet
Author: User
Tags exception handling lua
PHP5.5 just recently released, what are the new features inside? The official documentation is here:
http://www.php.net/manual/zh/migration55.new-features.php
1 Generator yield keywords
Yield's Chinese document is here: http://php.net/manual/zh/language.generators.overview.php
Looking at the document, you know that one feature of yield is the ability to effectively reduce the memory overhead of an iteration. For example, the official website of this xrange example:
Copy Code code as follows:

<?php
function xrange ($start, $limit, $step = 1) {
for ($i = $start; $i <= $limit; $i = = $step) {
Yield $i;
}
}

echo ' single digit odd numbers: ';

/*
* Note this is never created or returned,
* Which saves memory.
*/
foreach (Xrange (1, 9, 2) as $number) {
echo "$number";
}

echo "\ n";
?>

The xrange here is an iteration, function and range is the same, if the range function, then the internal implementation of the function will store the intermediate process of each iteration, that is, each intermediate variable has a memory space, then the first program to use a large memory space, and allocate memory, Reclaiming memory can cause the program to run longer. But if you use the Xrange function implemented on the yield, all of the intermediate variables in it will use only one memory $i, which will save you less time and space.

So why does yield have this effect? Lenovo's yield in Lua is a concept of the coprocessor. In the Lua language, when a program runs to yield, it records the context environment using a coprocessor, and then returns the program operation right to the main function, and when the main function calls resume, the process is revived and the context of the yield record is read. This forms a program language-level multi-process operation. PHP 5.5 Here yield is also the same truth, when the program runs to yield, the current program to evoke the context of the process, and then the main function continues to operate, but PHP does not use such as resume keyword, but "in the use of the time to arouse the" coprocessor. For example, the foreach iterator in the previous example can evoke yield. So this example above can be understood.

In fact, according to the reference yield, a lot of internal functions, especially the functions of the iteration should be optimized. Perhaps later there will be a yield version and a yield version of the function to achieve the same function.

2 finally keyword
This and finally in Java, the classic try ... catch ... finally three-segment exception handling.

3 foreach Support list ()
For an iteration of an array of arrays, you need to use two foreach before, but now you only need to use the Foreach + list, but the number of each array in the array of arrays needs to be the same. Look at the example of the document to see it.
Copy Code code as follows:

<?php
$array = [
[1, 2],
[3, 4],
];

foreach ($array as list ($a, $b)) {
echo "A: $a; B: $b \ n ";
}
?>

4 Empty () supports custom functions.
The parameters in empty () before are not functions. It's ok now
Copy Code code as follows:

<?php
function foo () {
return false;
}

if (Empty ()) (foo ()) {
Echo 11;
} else {
Echo 12;
}

5 non-variable array and string can also support subscripts to obtain the
Copy Code code as follows:

<?php

echo Array (1, 2, 3) [0];
echo [1, 2, 3][0];

echo "Foobar" [2];

?>

6 class name through:: Class can get
Copy Code code as follows:

<?php
namespace Name\space;
Class ClassName {}

Echo Classname::class;

echo "\ n";
?>

7 Increased Opcache expansion
Using Opcache will improve PHP performance, and you can add static compilation (--enable-opcache) or dynamic extensions (zend_extension) with other extensions to this optimization.

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.