SoFunction
Updated on 2025-03-10

Parse new features of PHP 5.5

PHP5.5 was just released not long ago. What are the new features in it? The official documentation is here:
/manual/zh/1 Generator yield keyword
Yield's Chinese document is here: /manual/zh/
By looking at the document, you can know that one of the functions of yield is to effectively reduce the memory overhead of iteration. For example, this xrange example from the official website:
Copy the codeThe code is as follows:

<?php
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";
?>

The xrange here is an iteration, and the 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 a memory space. First of all, the memory space used by the program will be large, and allocating memory and reclaiming memory will cause the program to run longer. However, if you use the xrange function implemented by yield, all the intermediate variables in it only use one memory $i, which will reduce the time and space.

So why does yield have such an effect? Thinking of yield in lua, this is considered a concept of coroutines. In the lua language, when the program runs to yield, the 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-called and the context of the yield record will be read. This creates multi-coroutine operations 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 calls the coroutine to record the context, and then the main function continues to operate. However, the php does not use keywords like resume, but "calls up" the coroutine when used. 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 yield, many internal functions, especially iteration-related functions, should be possible to optimize. Perhaps there will be functions that implement the same function in the future with the yield version and the non-yield version.

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

3 foreach supports list()
For iterating on "array of arrays", two foreaches were needed before, but 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 see the examples in the document at a glance.
Copy the codeThe code is 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 could not be functions. It's OK now
Copy the codeThe code is as follows:

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

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

5 Non-variable array and string can also support subscript acquisition
Copy the codeThe code is as follows:

<?php

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

echo "foobar"[2];

?>

6 Class names can be obtained through::class
Copy the codeThe code is as follows:

<?php
namespace Name\Space;
class ClassName {}

echo ClassName::class;

echo "\n";
?>

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