首页 > 编程 > PHP > 正文

PHP 5.5.0 Release note- support Zend OPcache

2020-03-22 18:08:29
字体:
来源:转载
供稿:网友
  • 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,简单举个例子

    <?phpfunction 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 $html' target='_blank'>value) {    echo "$value/n";}?>

    虽然有了 generators,但是我们可以来比较看看 generators 和 Iterator objects 的差异,网址例子来解释开启档案的程序代码

    <?phpfunction getLinesFromFile($fileName) {    if (!$fileHandle = fopen($fileName, 'r')) {        return;    }     while (false !== $line = fgets($fileHandle)) {        yield $line;    }     fclose($fileHandle);}// versus...class LineIterator implements Iterator {    protected $fileHandle;     protected $line;    protected $i;     public function __construct($fileName) {        if (!$this->fileHandle = fopen($fileName, 'r')) {            throw new RuntimeException('Couldn/'t open file "' . $fileName . '"');        }    }     public function rewind() {        fseek($this->fileHandle, 0);        $this->line = fgets($this->fileHandle);        $this->i = 0;    }     public function valid() {        return false !== $this->line;    }     public function current() {        return $this->line;    }     public function key() {        return $this->i;    }     public function next() {        if (false !== $this->line) {            $this->line = fgets($this->fileHandle);            $this->i++;        }    }     public function __destruct() {        fclose($this->fileHandle);    }}?>

    两者差异其实很清楚,如果是用 generators,是无法回复上一步骤,但是如果是用 Class 写法,就可以重复使用,只需要宣告一次即可,但是 generators 就必需要一直呼叫 function 来使用。

    新增 finally keyword
    直接来看网址的例子比较清楚

    <?phpfunction inverse($x) {    if (!$x) {        throw new Exception('Division by zero.');    }    return 1/$x;}try {    echo inverse(5) . "/n";} catch (Exception $e) {    echo 'Caught exception: ',  $e->getMessage(), "/n";} finally {    echo "First finally./n";}try {    echo inverse(0) . "/n";} catch (Exception $e) {    echo 'Caught exception: ',  $e->getMessage(), "/n";} finally {    echo "Second finally./n";}// Continue executionecho "Hello World/n";?>

    上面执行后会输出

    0.2First finally.Caught exception: Division by zero.Second finally.Hello World

    finally try cache 执行完毕后一定会执行到 finally block,另外在留言部份有个例子也很棒

    <?phptry{        try {                throw new /Exception("Hello");        } catch(/Exception $e) {                echo $e->getMessage()." catch in/n";                throw $e;        } finally {                echo $e->getMessage()." finally /n";                throw new /Exception("Bye");        }} catch (/Exception $e) {        echo $e->getMessage()." catch out/n";}?>

    新增 Password Hashing API

    password hashing API 提供了 crypt() 算法加密,所以现在不用自己写 Password hash 了,直接使用 PHP 内建吧

    <?php
    echo password_hash("rasmuslerdorf", PASSWORD_DEFAULT)."/n";输出为

    $2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a如果使用 CRYPT 算法

    <?php/** * In this case, we want to increase the default cost for BCRYPT to 12. * Note that we also switched to BCRYPT, which will always be 60 characters. */$options = [    'cost' => 12,];echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options)."/n";?>

    输出为

    $2y$12$QjSH496pcT5CEbzjD/vtVeH03tfHKFy36d4J0Ltp3lRtee9HDxY3K

    那该如何验证使用者登入密码正确呢?可以透过 password_verify function

    <?php// See the password_hash() example to see where this came from.$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';if (password_verify('rasmuslerdorf', $hash)) {    echo 'Password is valid!';} else {    echo 'Invalid password.';}?>

    empty() supports arbitrary expressions
    empty() 开始支持 function return value,不只是判断变量而已。

    <?phpfunction always_false() {    return false;}if (empty(always_false())) {    echo "This will be printed./n";}if (empty(true)) {    echo "This will not be printed./n";}?>

    如果在 5.5 版本以前就会喷

    PHP Fatal error: Can’t use function return value in write context

    支援 ::class keyword
    5.5 开始支持 Class name resolution

    <?php
    namespace NS {
    class ClassName {
    }

    echo ClassName::class;
    }
    ?>

    上述输出为

    NS/ClassNameforeach 支持阶乘式数组
    <?php
    $array = [
    [1, 2],
    [3, 4],
    ];

    foreach ($array as list($a, $b)) {
    // $a contains the first element of the nested array,
    // and $b contains the second element.
    echo "A: $a; B: $b/n";
    }
    ?>

    输出为

    A: 1; B: 2
    A: 3; B: 4真的是太强大了。

    内建 Zend OPcache
    本篇重点就是 5.5 开始内建 Zend OPcache,在之前要 tune php performance,无非就是加上 APC,为了改善 cache 效能,Zend 官方又写了一套 ZendOptimizerPlus,将 PHP 编译程 bytecode 存放在 shared memory,此方式避免到硬盘读取 PHP 程序代码,并且编译再执行。www.it165.net

    目前支持 PHP 5.2.*, 5.3.*, 5.4.* and PHP-5.5,但是也许 PHP 5.2.* 将来会拔掉,但是这还没确定。如果您的系统是 php5.5 以前的版本,可以透过 pecl 方式来安装

    # support Zend OPcache on PHP 5.2, 5.3 and 5.4
    pecl install channel://pecl.php.net/ZendOpcache-7.0.2完成后新增设定档 /etc/php5/cli/conf.d/10-opcache.ini

    zend_extension=opcache.so

    [opcache]
    opcache.memory_consumption=128
    opcache.interned_strings_buffer=8
    opcache.max_accelerated_files=4000
    opcache.revalidate_freq=60
    opcache.fast_shutdown=1
    opcache.enable_cli=1

    需要注意的是 opcache.revalidate_freq 设定,预设是2秒,也就是两秒内 opcache 不会去侦测程序代码是否有修改,如果正在开发状态,并且搭配 Livereload 的话,使用 opcache 请把 revalidate_freq 设定为 0,让每次 reload 都重新侦测程序代码是否改变。

    PHP编程

    郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表