1、新增 Traits
https://php.net/manual/zh/language.oop5.traits.php
// Traits不能被单独实例化,只能被类所包含 trait SayWorld { public function sayHello() { echo 'World!'; } } class MyHelloWorld { // 将SayWorld中的成员包含进来 use SayWorld; } $xxoo = new MyHelloWorld(); // sayHello() 函数是来自 SayWorld 构件的
2、新增短数组语法
// 原来的数组写法 $arr = array("key" => "value", "key2" => "value2"); $arr = array(1, 2, 3, 4); // 简写形式 $arr = ["key" => "value", "key2" => "value2"]; $arr = [1, 2, 3, 4];
3、新增支持对函数返回数组的成员访问解析
print func()[0];
4、无论 php.ini 中是否设置 short_open_tag, 格式总是可用。
这种简写形式被称为 Short Open Tag, 在 PHP5.3 起被默认开启,在 PHP5.4 起总是可用。 使用这种简写形式在 HTML 中嵌入 PHP 变量将会非常方便。
5、内置用于开发的 CLI 模式的 web server
//启动Web服务器 php -S localhost:8000 //启动时指定根目录 php -S localhost:8000 -t /home/me/public_html/foo //使用路由(Router)脚本 php -S localhost:8000 index.php //所有的请求都会由index.php来处理。
6、新增在实例化时访问类成员
(new Foo)->bar();
7、新增了动态访问静态方法的方式
$func = "funcXXOO"; A::{$func}();
8、闭包支持 $this
9、新增二进制直接量
$bin = bindec('110011'); //之前需要这样写 $bin = 0b110011; echo $bin; //51
10、session提供了上传进度支持
通过 $_SESSION[“upload_progress_name”] 就可以获得当前文件上传的进度信息,结合 Ajax 就能很容易的实现上传进度条。
11、默认使用 mysqlnd
现在mysql, mysqli, pdo_mysql默认使用mysqlnd本地库,在PHP5.4以前需要:./configure –with-mysqli=mysqlnd
现在:./configure –with-mysqli
12、让 json 更懂中文
echo json_encode("中文", JSON_UNESCAPED_UNICODE); //"中文"
13、default_charset从ISO-8859-1已经变为UTF-8
默认发送“Content-Type: text/html; charset=utf-8”
转载请注明:隨習筆記 » PHP 5.4 新特性