Heim  >  Artikel  >  Backend-Entwicklung  >  对于PHP 5.4 你必须要知道的_PHP教程

对于PHP 5.4 你必须要知道的_PHP教程

WBOY
WBOYOriginal
2016-07-21 14:59:23900Durchsuche

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)原则。

复制代码 代码如下:

/** cURL wrapper trait */
trait cURL
{
public function curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}
/** Twitter API Class */
class Twitter_API
{
use cURL; // use trait here
public function get($url)
{
return json_decode($this->curl('http://api.twitter.com/'.$url));
}
}
/** Facebook API Class */
class Facebook_API
{
use cURL; // and here
public function get($url)
{
return json_decode($this->curl('http://graph.facebook.com/'.$url));
}
}
$facebook = new Facebook_API();
echo $facebook->get('500058753')->name; // Rasmus Lerdorf
/** Now demonstrating the awesomeness of PHP 5.4 syntax */
echo (new Facebook_API)->get('500058753')->name;
$foo = 'get';
echo (new Facebook_API)->$foo('500058753')->name;
echo (new Twitter_API)->get('1/users/show.json?screen_name=rasmus')->name;

看明白了吗?没有?那你来瞅瞅更简单的例子
复制代码 代码如下:

trait Hello
{
public function hello()
{
return 'Hello';
}
}
trait Cichui
{
public function cichui()
{
return ' cichui';
}
}
class HelloCichui
{
use Hello, Cichui;
public function the_end()
{
return '!';
}
}
$o = new HelloCichui;
echo $o->hello(), $o->cichui(), $o->the_end();
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文件,把以下代码复制粘贴进去:
复制代码 代码如下:

 // router.php
if (preg_match('#\.php$#', $_SERVER['REQUEST_URI']))
{
require basename($_SERVER['REQUEST_URI']); // serve php file
}
else if (strpos($_SERVER['REQUEST_URI'], '.') !== false)
{
return false; // serve file as-is
}
?>

再来新建一个index.php文件,复制粘贴以下代码:
// index.php
echo 'Hello cichui.com Readers!';
?>
编辑你的php.ini文件,找到”include_path”一行,把c:\public_html添加进去(分号分隔):
1include_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数组语法:

复制代码 代码如下:

$fruits = array('apples', 'oranges', 'bananas'); // "old" way
// 学Javascript的数组了
$fruits = ['apples', 'oranges', 'bananas'];
// 关联数组
$array = [
'foo' => 'bar',
'bar' => 'foo'
];

当然,旧语法依旧有效,我们多了一种选择。
数组成员访问解析(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
再来个高级点的例子:

复制代码 代码如下:

function foobar()
{
return ['foo' => ['bar' => 'Hello']];
}
echo foobar()['foo']['bar']; // Hello

*瓷锤注: Array dereferencing直译应为数组解除引用,效果不佳。其实更准确的翻译应为:“对函数返回结果的数组成员访问解析支持”,详见PHP官方解释。

匿名函数中的$this
现在,你可以在类实例中通过$this引用一个匿名函数(也叫闭包函数)

复制代码 代码如下:

class Foo
{
function hello() {
echo 'Hello Cichui!';
}
function anonymous()
{
return function() {
$this->hello(); // 之前是不可能这么玩的
};
}
}
class Bar
{
function __construct(Foo $o) // object of class Foo typehint
{
$x = $o->anonymous(); // get Foo::hello()
$x(); // execute Foo::hello()
}
}
new Bar(new Foo); // Hello Cichui!
其实以前也能将就用,就是有点费劲: 
function anonymous()
{
$that = $this; // $that is now $this
return function() use ($that) {
$that->hello();
};
}

无论php.ini中如何配置,short_open_tag, 也就是 替换以前的了。

支持二进制直接量
八进制(oct),前面加0;十六进制(hex),前面加0x;二进制(bin),现在在前面加0b就可以了
echo 0b11111; // PHP 5.4支持二进制了
echo 31; // 十进制
echo 0x1f; // 十六进制
echo 037; // 八进制

函数类型提示
自PHP 5.1起,类型提示支持对象和数组,PHP 5.4开始支持callable。

复制代码 代码如下:

function my_function(callable $x)
{
return $x();
}
function my_callback_function(){return 'Hello Cichui!';}
class Hello{static function hi(){return 'Hello Cichui!';}}
class Hi{function hello(){return 'Hello Cichui!';}}
echo my_function(function(){return 'Hello Cichui!';}); // 闭包函数
echo my_function('my_callback_function'); // 回调函数
echo my_function(['Hello', 'hi']); // 类名,静态方法
echo my_function([(new Hi), 'hello']); // 类名,方法名

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

小结
总之,此次PHP 5.4升级进行大量的改动。 是时候升级了。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/328139.htmlTechArticlePHP 5.4来了,这是自5.3后的又一次主版本升级。此次升级改动较为显著,删除了一些过气儿的函数,带来了高达20%的速度提升和更少的内存使...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn