找回密码
 注册
搜索
查看: 2807|回复: 1

关于 PHP 5.4 你所需要知道的

[复制链接]
发表于 2012-3-29 15:05:59 | 显示全部楼层 |阅读模式
PHP 5.4来了,这是自5.3后的又一次主版本升级。此次升级改动较为显著,删除了一些过气儿的函数,带来了高达20%的速度提升和更少的内存使用。

新特性与改动
此次更新的关键新特性,包括:新增traits,更精简的Array数组语法,供测试使用的内建webserver,可以闭包使用的$this指针,实例化类成员访问,<?=标记永远生效 等等……
PHP 5.4.0 性能大幅提升, 修复超过100个bug. 废除了register_globals, magic_quotes以及安全模式。 另外值得一提的是多字节支持已经默认启用了,default_charset从ISO-8859-1已经变为UTF-8. 默认发送“Content-Type: text/html; charset=utf-8”,你再也不需要在HTML里写meta tag,也无需为UTF-8兼容而传送额外的header了。
Traits Traits (横向重用/多重继承)是一组结构很像“类”(但不能实例化)的方法,它可以让开发人员在不同的类中轻松地重用方法。 PHP为单继承语言,子类只能继承一个父类,于是Traits来了。
Traits的最佳应用是多类之间可以共享相同的函数。打个比方,我们要做个网站,需要使用Facebook和Twitter的APIs。我们要建2个类,如果是以前,我们需要写一个cURL的方法并且复制/粘贴到两个类中。现在不用了,使用Traits重用代码吧,这次真正地遵循了DRY(Don’t Repeat Yourself)原则。
  1. /** cURL wrapper trait */
  2. trait cURL
  3. {
  4.     public function curl($url)
  5.     {
  6.         $ch = curl_init();
  7.         curl_setopt($ch, CURLOPT_URL, $url);
  8.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  9.         $output = curl_exec($ch);
  10.         curl_close($ch);
  11.         return $output;
  12.     }
  13. }

  14. /** Twitter API Class */
  15. class Twitter_API
  16. {
  17.     use cURL; // use trait here
  18.     public function get($url)
  19.     {
  20.         return json_decode($this->curl('http://api.twitter.com/'.$url));
  21.     }
  22. }

  23. /** Facebook API Class */
  24. class Facebook_API
  25. {
  26.     use cURL; // and here
  27.     public function get($url)
  28.     {
  29.         return json_decode($this->curl('http://graph.facebook.com/'.$url));
  30.     }
  31. }

  32. $facebook = new Facebook_API();
  33. echo $facebook->get('500058753')->name; // Rasmus Lerdorf

  34. /** Now demonstrating the awesomeness of PHP 5.4 syntax */
  35. echo (new Facebook_API)->get('500058753')->name;
  36. $foo = 'get';
  37. echo (new Facebook_API)->$foo('500058753')->name;
  38. echo (new Twitter_API)->get('1/users/show.json?screen_name=rasmus')->name;
复制代码
看明白了吗?没有?那你来瞅瞅更简单的例子
  1. trait Hello
  2. {
  3.     public function hello()
  4.     {
  5.         return 'Hello';
  6.     }
  7. }

  8. trait Cichui
  9. {
  10.     public function cichui()
  11.     {
  12.         return ' cichui';
  13.     }
  14. }

  15. class HelloCichui
  16. {
  17.     use Hello, Cichui;
  18.     public function the_end()
  19.     {
  20.         return '!';
  21.     }
  22. }

  23. $o = new HelloCichui;
  24. echo $o->hello(), $o->cichui(), $o->the_end();
  25. echo (new Hello)->hello(), (new Cichui)->cichui(), (new HelloCichui)->the_end();
复制代码

内建的Web-Sever
在Web开发中,Apache HTTPD是PHP的最佳拍档。有时,你开发时用不上需要配置httpd.conf的apache大杀器,而只需要一个可以在命令行中使用的超小型Webserver. 感谢PHP(先感谢国家),PHP 5.4这次内建了CLI Web server。(PHP CLI webserver仅供开发使用,谢绝产品用途)
举个栗子(windows平台):

步骤一:建立web根目录, Router和Index

在硬盘根目录(比如C盘)建立一个public_html目录,目录里新建一个router.php文件,把以下代码复制粘贴进去:
  1. <?php
  2. // router.php
  3. if (preg_match('#\.php$#', $_SERVER['REQUEST_URI']))
  4. {
  5.     require basename($_SERVER['REQUEST_URI']); // serve php file
  6. }
  7. else if (strpos($_SERVER['REQUEST_URI'], '.') !== false)
  8. {
  9.     return false; // serve file as-is
  10. }
  11. ?>
复制代码
再来新建一个index.php文件,复制粘贴以下代码:
  1. <?php
  2. // index.php
  3. echo 'Hello cichui.com Readers!';
  4. ?>
复制代码
编辑你的php.ini文件,找到”include_path”一行,把c:\public_html添加进去(分号分隔):
  1. include_path = ".;C:\php\PEAR;C:\public_html"
复制代码
存盘退出,看下一步

步骤二:运行Web-Server

切换到php的安装目录,敲下最关键的命令—运行Web-server
php -S 0.0.0.0:8080 -t C:\public_html router.php

开始了吗?不要关闭窗口,如果进程关闭Web server也跟着关闭了。
打开浏览器:访问http://localhost:8080/index.php吧,
Hello cichui.com Readers!
看到了吧?对,就是这个!
提示1:你可以考虑自建一个php-server.bat的批处理,扔到桌面上以后就可以双击启动了。
提示2:使用0.0.0.0而不是localhost,可以保证外网不会访问到你的web serve。
精简的Array数组语法 PHP 5.4为您奉上精简的array数组语法:
  1. $fruits = array('apples', 'oranges', 'bananas'); // "old" way

  2. // 学Javascript的数组了
  3. $fruits = ['apples', 'oranges', 'bananas'];

  4. // 关联数组
  5. $array = [
  6.     'foo' => 'bar',
  7.     'bar' => 'foo'
  8. ];
复制代码

当然,旧语法依旧有效,我们多了一种选择。
数组成员访问解析(Array dereferencing*) 处理数组再也不需要临时变量了。
假设我们需要获取Fang Bin Xin的middle name,
echo explode(‘ ‘, ‘Fang Bin Xin’)[1]; // Bin
PHP 5.4之前,我们需要这样:
$tmp = explode(‘ ‘, ‘Fang Bin Xin’);
echo $tmp[1]; // Bin
现在,我们可以这样玩了:
echo end(explode(‘ ‘, ‘Fang Bin Xin’)); // Xin
再来个高级点的例子:
  1. function foobar()
  2. {
  3.     return ['foo' => ['bar' => 'Hello']];
  4. }
  5. echo foobar()['foo']['bar']; // Hello
复制代码
瓷锤注: Array dereferencing直译应为数组解除引用,效果不佳。其实更准确的翻译应为:“对函数返回结果的数组成员访问解析支持”,详见PHP官方解释
匿名函数中的$this 现在,你可以在类实例中通过$this引用一个匿名函数(也叫闭包函数)
  1. class Foo
  2. {
  3.     function hello() {
  4.         echo 'Hello Cichui!';
  5.     }

  6.     function anonymous()
  7.     {
  8.         return function() {
  9.             $this->hello(); // 之前是不可能这么玩的
  10.         };
  11.     }
  12. }

  13. class Bar
  14. {
  15.     function __construct(Foo $o) // object of class Foo typehint
  16.     {
  17.         $x = $o->anonymous(); // get Foo::hello()
  18.         $x(); // execute Foo::hello()
  19.     }
  20. }
  21. new Bar(new Foo); // Hello Cichui!
复制代码
其实以前也能将就用,就是有点费劲:
  1. function anonymous()
  2. {
  3.     $that = $this; // $that is now $this
  4.     return function() use ($that) {
  5.         $that->hello();
  6.     };
  7. }
复制代码
<?= 永远有效 无论php.ini中如何配置,short_open_tag, 也就是<?=(php标记+echo)永远有效。现在,你可以安全地使用 <?=$title?> 替换以前的<?php echo $title;?>了。
支持二进制直接量 八进制(oct),前面加0;十六进制(hex),前面加0x;二进制(bin),现在在前面加0b就可以了
  1. echo 0b11111; // PHP 5.4支持二进制了
  2. echo 31; // 十进制
  3. echo 0x1f; // 十六进制
  4. echo 037; // 八进制
复制代码

函数类型提示 自PHP 5.1起,类型提示支持对象和数组,PHP 5.4开始支持callable。
  1. function my_function(callable $x)
  2. {
  3.     return $x();
  4. }

  5. function my_callback_function(){return 'Hello Cichui!';}

  6. class Hello{static function hi(){return 'Hello Cichui!';}}
  7. class Hi{function hello(){return 'Hello Cichui!';}}

  8. echo my_function(function(){return 'Hello Cichui!';}); // 闭包函数
  9. echo my_function('my_callback_function'); // 回调函数
  10. echo my_function(['Hello', 'hi']); // 类名,静态方法
  11. echo my_function([(new Hi), 'hello']); // 类名,方法名
复制代码

高精度计时器 此次引入了$_SERVER['REQUEST_TIME_FLOAT']数组变量,微秒级精度(百万分之一秒,float类型)。对于统计脚本运行时间会非常有用:
  1. echo 'Executed in ', round(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], 2)
复制代码

小结 总之,此次PHP 5.4升级进行大量的改动。 是时候升级了。
PHP 5.5又将给我们带来什么,你又有那些期待呢?


原文:http://www.cnbeta.com/articles/179558.htm
发表于 2012-4-3 06:43:33 | 显示全部楼层
我改用zendserv CE搭建的php环境,右上角玩家信息栏就出现错误:Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, admin@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|宁德市腾云网络科技有限公司 ( 闽ICP备2022007940号-5|闽公网安备 35092202000206号 )

GMT+8, 2025-6-19 04:05 , Processed in 0.017243 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表