PHP 5.5.0 Release note: support Zend OPcache

php-logo

PHP 5.5.0 在上週 20 號正式 Release,也看到 PHP 官網終於改版了,新的版面看起來比較清爽,想嘗試新版面的朋友們,可以點選官網最上面鎖提示的 Bar,如果覺得新版面不是很好看,也可以切回去舊版。本篇來介紹 PHP 5.5.0 有哪些新 Feature。

新增 generators and coroutines 功能

Generators 提供了最簡單的寫法來實做 iterators,而不需要實做 Class 去實做 Iterator 介面,generators function 就跟一般的 PHP function 一樣,只是多了 yield 這 keyword,簡單舉個例子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?php
function gen_one_to_three() {
    for ($i = 1; $i <= 3; $i++) {
        // Note that $i is preserved between yields.
        yield $i;
    }
}

$generator = gen_one_to_three();
foreach ($generator as $value) {
    echo "$value\n";
}
?>
[Read More]