zoukankan      html  css  js  c++  java
  • PHP5.4新特性(转)

    PHP5.4正式前两天发布了,之前有看了一些PHP5.4主要特性相关文章,因此在这里小结一下。

    其中好几点更新是由Laruence贡献的!本文部分内容也是源自Laruence的博客。

    1. Buid-in web server
    PHP5.4内置了一个简单的Web服务器,这样在做一些简单程序就方便多了,省去了环境配置的工作,特别对于初学者来说。
    把当前目录作为Root Document只需要这条命令即可:

    1
    $ php -S localhost:3300

    也可以指定其它路径:

    1
    $ php -S localhost:3300 -t /path/to/root

    还可以指定路由:

    1
    $ php -S localhost:3300 router.php

    参考:PHP: Build-in web server

    2. Traits
    Traits提供了一种灵活的代码重用机制,即不像interface一样只能定义方法但不能实现,又不能像class一样只能单继承。至于在实践中怎样使用,还需要深入思考。
    官网的一个例子:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    trait SayWorld {
            public function sayHello() {
                    parent::sayHello();
                    echo "World! ";
                    echo 'ID:' . $this->id . " ";
            }
    }
     
    class Base {
            public function sayHello() {
                    echo 'Hello ';
            }
    }
      
    class MyHelloWorld extends Base {
            private $id;
      
            public function __construct() {
                    $this->id = 123456;
            }
      
            use SayWorld;
    }
      
    $o = new MyHelloWorld();
    $o->sayHello();
      
    /*will output:
    Hello World!
    ID:123456
     */

    参考:http://cn.php.net/manual/en/language.oop5.traits.php

    3. Short array syntax
    PHP5.4提供了数组简短语法:

    1
    $arr = [1,'james', 'james@fwso.cn'];

    4. Array dereferencing

    1
    2
    3
    function myfunc() {
        return array(1,'james', 'james@fwso.cn');
    }

    我认为比数组简短语法更方便的是dereferencing,以前我们需要这样:

    1
    2
    $arr = myfunc();
    echo $arr[1];

    在PHP5.4中这样就行了:

    1
    echo myfunc()[1];

    5. Upload progress
    Session提供了上传进度支持,通过$_SESSION["upload_progress_name"]就可以获得当前文件上传的进度信息,结合Ajax就能很容易实现上传进度条了。

    参考:http://www.laruence.com/2011/10/10/2217.html

    6. JsonSerializable Interface
    实现了JsonSerializable接口的类的实例在json_encode序列化的之前会调用jsonSerialize方法,而不是直接序列化对象的属性。
    参考:http://www.laruence.com/2011/10/10/2204.html

    7. Use mysqlnd by default
    现在mysql, mysqli, pdo_mysql默认使用mysqlnd本地库,在PHP5.4以前需要:

    1
    $./configure --with-mysqli=mysqlnd

    现在:

    1
    $./configure --with-mysqli

    8. 更多
    http://cn2.php.net/releases/NEWS_5_4_0_beta2.txt

  • 相关阅读:
    poj2096(概率dp)
    bzoj4318/洛谷P1654OSU!(期望dp,立方版本)
    hdu1027(逆康托展开)
    hdu3734(数位dp,相减抵消影响)
    hdu2089(数位dp模版)
    hdu2856(倍增lca模版题)
    COI2007 Patrik 音乐会的等待 洛谷P1823
    校门外的树2 contest 树状数组练习 T4
    数星星 contest 树状数组练习 T2
    A simple problem with integer 树状数组区间查询模板题 contest 树状数组练习 T1
  • 原文地址:https://www.cnblogs.com/xingmeng/p/3186595.html
Copyright © 2011-2022 走看看