Home > Backend Development > PHP Tutorial > Analyzing PHP 5.5 New Features_PHP Tutorial

Analyzing PHP 5.5 New Features_PHP Tutorial

WBOY
Release: 2016-07-21 15:01:59
Original
850 people have browsed it

PHP5.5 was released not long ago. What are the new features in it? The official document is here:
http://www.php.net/manual/zh/migration55.new-features.php
1 Generator yield keyword
The Chinese documentation of yield is here: http://php.net/manual/zh/language.generators.overview.php
Looking at the documentation, you can know that one of the functions of yield is to effectively reduce the memory overhead of iteration . For example, this xrange example on the official website:

Copy the code The code is as follows:

function xrange($ start, $limit, $step = 1) {
for ($i = $start; $i <= $limit; $i += $step) {
yield $i;
}
}

echo 'Single digit odd numbers: ';

/*
* Note that an array is never created or returned,
* which saves memory.
*/
foreach (xrange(1, 9, 2) as $number) {
echo "$number ";
}

echo "n";
?> ;

xrange here is an iteration, and its function is the same as range. If the range function is used, the internal implementation of the function will store the intermediate process of each iteration, that is, each intermediate variable has If there is a memory space, first of all, the memory space used by the program will be large, and allocating memory and recycling memory will lengthen the running time of the program. But if you use the xrange function implemented by yield, all intermediate variables in it only use one memory $i, so the time and space saved will become smaller.

So why does yield have such an effect? Thinking of yield in Lua, here is the concept of coroutine. In the Lua language, when the program reaches yield, a coroutine is used to record the context environment, and then the program operation rights are returned to the main function. When the main function calls resume, the coroutine will be re-awakened and the yield record will be read. context. This forms a multi-coroutine operation at the programming language level. The same is true for yield here in PHP 5.5. When the program runs to yield, the current program evokes the coroutine to record the context, and then the main function continues to operate. However, keywords such as resume are not used in PHP, but "using When the coroutine is called up. For example, the foreach iterator in the above example can evoke yield. So the above example can be understood.

In fact, according to the reference to yield, many internal functions, especially those related to iteration, should be able to be optimized. Maybe there will be yield and non-yield versions of functions that implement the same function in the future.

2 finally keyword
This is the same as finally in java, the classic try...catch...final three-stage exception handling.

3 foreach supports list()
For "array of arrays" iteration, you need to use two foreach before, now you only need to use foreach + list , but the number of each array in the array of this array needs to be the same. You can understand it at a glance by looking at the examples in the documentation.
Copy code The code is as follows:

$array = [
[1, 2 ],
[3, 4],
];

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

4 empty() now supports custom functions
before empty() The parameters in cannot be functions. Now it works
Copy the code The code is as follows:

function foo(){
return false;
}

if(empty(foo())){
echo 11;
} else {
echo 12;
}

5 Non-variable arrays and strings can also support subscript acquisition
Copy code Code As follows:


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

echo "foobar"[2];

?>

6 The class name can be obtained through ::class
Copy code The code is as follows:

namespace NameSpace;
class ClassName { }

echo ClassName::class;

echo "n";
?>

7 Added opcache extension
Using opcache will improve the performance of PHP. You can statically compile (--enable-opcache) or dynamically expand ( zend_extension) to add this optimization item.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327953.htmlTechArticlePHP5.5 was just released not long ago. What are the new features in it? The official document is here: http://www.php.net/manual/zh/migration55.new-features.php 1 Generator yield keyword yield...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template