时间:2014-02-15所属栏目: PHP/PHP框架 来源: 未知 热度:
PHP5.4正式前两天宣布了,之前有看了一些PHP5.4首要特征相干文章,因此在这里小结一下。
个中好几点更新是由Laruence孝顺的!本文部门内容也是源自Laruence的博客。
1. Buid-in web server
PHP5.4内置了一个简朴的Web处事器,这样在做一些简朴措施就利便多了,省去了情形设置的事变,出格对付初学者来说。
把当前目次作为Root Document只必要这条呼吁即可:
$ php -S localhost:3300
也可以指定其余路径:
$ php -S localhost:3300 -t /path/to/root
还可以指定路由:
$ php -S localhost:3300 router.php
参考:PHP: Build-in web server
2. Traits
Traits提供了一种机动的代码重用机制,即不像interface一样只能界说要领但不能实现,又不能像class一样只能单担任。至于在实践中奈何行使,还必要深入思索。
官网的一个例子:
trait SayWorld {
public function sayHello() {
parent::sayHello();
echo "World!\n";
echo 'ID:' . $this->id . "\n";
}
}
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提供了数组简短语法:
$arr = [1,'james', 'james@fwso.cn'];
4. Array dereferencing
function myfunc() {
return array(1,'james', 'james@fwso.cn');
}
我以为比数组简短语法更利便的是dereferencing,早年我们必要这样:
$arr = myfunc(); echo $arr[1];
在PHP5.4中这样就行了:
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早年必要:
$./configure --with-mysqli=mysqlnd
此刻:
$./configure --with-mysqli
8. 更多
http://cn2.php.net/releases/NEWS_5_4_0_beta2.txt