|
php 5.4來(lái)了,這是自5.3后的又一次主版本升級(jí)。此次升級(jí)改動(dòng)較為顯著,刪除了一些過(guò)氣兒的函數(shù),帶來(lái)了高達(dá)20%的速度提升和更少的內(nèi)存使用。
新特性與改動(dòng)
此次更新的關(guān)鍵新特性,包括:新增traits,更精簡(jiǎn)的Array數(shù)組語(yǔ)法,供測(cè)試使用的內(nèi)建webserver,可以閉包使用的$this指針,實(shí)例化類(lèi)成員訪問(wèn),
php 5.4.0 性能大幅提升, 修復(fù)超過(guò)100個(gè)bug. 廢除了register_globals, magic_quotes以及安全模式。 另外值得一提的是多字節(jié)支持已經(jīng)默認(rèn)啟用了,default_charset從ISO-8859-1已經(jīng)變?yōu)閁TF-8. 默認(rèn)發(fā)送“Content-Type: text/html; charset=utf-8”,你再也不需要在HTML里寫(xiě)meta tag,也無(wú)需為UTF-8兼容而傳送額外的header了。
Traits
Traits (橫向重用/多重繼承)是一組結(jié)構(gòu)很像“類(lèi)”(但不能實(shí)例化)的方法,它可以讓開(kāi)發(fā)人員在不同的類(lèi)中輕松地重用方法。 php為單繼承語(yǔ)言,子類(lèi)只能繼承一個(gè)父類(lèi),于是Traits來(lái)了。
Traits的最佳應(yīng)用是多類(lèi)之間可以共享相同的函數(shù)。打個(gè)比方,我們要做個(gè)網(wǎng)站,需要使用Facebook和Twitter的APIs。我們要建 2個(gè)類(lèi),如果是以前,我們需要寫(xiě)一個(gè)cURL的方法并且復(fù)制/粘貼到兩個(gè)類(lèi)中。現(xiàn)在不用了,使用Traits重用代碼吧,這次真正地遵循了 DRY(Don't Repeat Yourself)原則。
復(fù)制代碼 代碼如下:
/** 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;
看明白了嗎?沒(méi)有?那你來(lái)瞅瞅更簡(jiǎn)單的例子
復(fù)制代碼 代碼如下:
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();
內(nèi)建的Web-Sever
在Web開(kāi)發(fā)中,Apache HTTPD是php的最佳拍檔。有時(shí),你開(kāi)發(fā)時(shí)用不上需要配置httpd.conf的apache大殺器,而只需要一個(gè)可以在命令行中使用的超小型 Webserver. 感謝php(先感謝國(guó)家),php 5.4這次內(nèi)建了CLI Web server。(php CLI webserver僅供開(kāi)發(fā)使用,謝絕產(chǎn)品用途)
舉個(gè)栗子(windows平臺(tái)):
步驟一:建立web根目錄, Router和Index
在硬盤(pán)根目錄(比如C盤(pán))建立一個(gè)public_html目錄,目錄里新建一個(gè)router.php文件,把以下代碼復(fù)制粘貼進(jìn)去:
復(fù)制代碼 代碼如下:
// 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
}
?>
再來(lái)新建一個(gè)index.php文件,復(fù)制粘貼以下代碼:
// index.php
echo 'Hello cichui.com Readers!';
?>
編輯你的php.ini文件,找到”include_path”一行,把c:/public_html添加進(jìn)去(分號(hào)分隔):
1include_path = ".;C:/php/PEAR;C:/public_html"
存盤(pán)退出,看下一步
步驟二:運(yùn)行Web-Server
切換到php的安裝目錄,敲下最關(guān)鍵的命令―運(yùn)行Web-server
php -S 0.0.0.0:8080 -t C:/public_html router.php
開(kāi)始了嗎?不要關(guān)閉窗口,如果進(jìn)程關(guān)閉Web server也跟著關(guān)閉了。
打開(kāi)瀏覽器:訪問(wèn)http://localhost:8080/index.php吧,
Hello cichui.com Readers!
看到了吧?對(duì),就是這個(gè)!
提示1:你可以考慮自建一個(gè)php-server.bat的批處理,扔到桌面上以后就可以雙擊啟動(dòng)了。
提示2:使用0.0.0.0而不是localhost,可以保證外網(wǎng)不會(huì)訪問(wèn)到你的web serve。
精簡(jiǎn)的Array數(shù)組語(yǔ)法
php 5.4為您奉上精簡(jiǎn)的array數(shù)組語(yǔ)法:
復(fù)制代碼 代碼如下:
$fruits = array('apples', 'oranges', 'bananas'); // "old" way
// 學(xué)Javascript的數(shù)組了
$fruits = ['apples', 'oranges', 'bananas'];
// 關(guān)聯(lián)數(shù)組
$array = [
'foo' => 'bar',
'bar' => 'foo'
];
當(dāng)然,舊語(yǔ)法依舊有效,我們多了一種選擇。
數(shù)組成員訪問(wèn)解析(Array dereferencing*)
處理數(shù)組再也不需要臨時(shí)變量了。
假設(shè)我們需要獲取Fang Bin Xin的middle name,
echo explode(‘ ‘, ‘Fang Bin Xin')[1]; // Bin
php 5.4之前,我們需要這樣:
$tmp = explode(‘ ‘, ‘Fang Bin Xin');
echo $tmp[1]; // Bin
現(xiàn)在,我們可以這樣玩了:
echo end(explode(‘ ‘, ‘Fang Bin Xin')); // Xin
再來(lái)個(gè)高級(jí)點(diǎn)的例子:
復(fù)制代碼 代碼如下:
function foobar()
{
return ['foo' => ['bar' => 'Hello']];
}
echo foobar()['foo']['bar']; // Hello
*瓷錘注: Array dereferencing直譯應(yīng)為數(shù)組解除引用,效果不佳。其實(shí)更準(zhǔn)確的翻譯應(yīng)為:“對(duì)函數(shù)返回結(jié)果的數(shù)組成員訪問(wèn)解析支持”,詳見(jiàn)php官方解釋。
匿名函數(shù)中的$this
現(xiàn)在,你可以在類(lèi)實(shí)例中通過(guò)$this引用一個(gè)匿名函數(shù)(也叫閉包函數(shù))
復(fù)制代碼 代碼如下:
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!
其實(shí)以前也能將就用,就是有點(diǎn)費(fèi)勁:
function anonymous()
{
$that = $this; // $that is now $this
return function() use ($that) {
$that->hello();
};
}
無(wú)論php.ini中如何配置,short_open_tag, 也就是 替換以前的了。
支持二進(jìn)制直接量
八進(jìn)制(oct),前面加0;十六進(jìn)制(hex),前面加0x;二進(jìn)制(bin),現(xiàn)在在前面加0b就可以了
echo 0b11111; // php 5.4支持二進(jìn)制了
echo 31; // 十進(jìn)制
echo 0x1f; // 十六進(jìn)制
echo 037; // 八進(jìn)制
函數(shù)類(lèi)型提示
自php 5.1起,類(lèi)型提示支持對(duì)象和數(shù)組,php 5.4開(kāi)始支持callable。
復(fù)制代碼 代碼如下:
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!';}); // 閉包函數(shù)
echo my_function('my_callback_function'); // 回調(diào)函數(shù)
echo my_function(['Hello', 'hi']); // 類(lèi)名,靜態(tài)方法
echo my_function([(new Hi), 'hello']); // 類(lèi)名,方法名
高精度計(jì)時(shí)器
此次引入了$_SERVER['REQUEST_TIME_FLOAT']數(shù)組變量,微秒級(jí)精度(百萬(wàn)分之一秒,float類(lèi)型)。對(duì)于統(tǒng)計(jì)腳本運(yùn)行時(shí)間會(huì)非常有用:
1echo 'Executed in ', round(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], 2)
小結(jié)
總之,此次php 5.4升級(jí)進(jìn)行大量的改動(dòng)。 是時(shí)候升級(jí)了。
php技術(shù):對(duì)于PHP 5.4 你必須要知道的,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。