PHP5.4を知らない人は要チェック。いやPHP6.0か?

最近PHP5.4が気になったので調査してみた。今(2010/9/25)はPHP5.3が最新だが次のバージョンアップではどうやらかなり変わるらしい。しかもPHP5.4にするかPHP6.0にするかまだ決まっていないので、今のtrunkにはphp5.3.99devというバージョンになっている。みんなも経験があると思うが、PHPはマイナーバージョンアップでもいきなり仕様が変わって困ることが多いので今回は恐ろしいことになりそうだ。

バージョンに関しては、自分的にはPHP6.0よりPHP5.4になる予感がするのであえてPHP5.4と書くことにする。検索するときは、PHP5.4よりもPHP6.0のほうが圧倒的に情報多いので調査のときは、PHP6.0で検索することをお勧めする。では一覧で紹介してくが、この情報はリポジトリにあったNEWSで、日々更新されるのでおおまかに説明してくことにする。

PHP5.4で何が変わるのか

dereferencingをサポート

(原文)
Added array dereferencing support. (Felipe)

JavaScriptでお馴染みのやつですね。

<?php
function getArray() {
    return array(“elem1”, “elem2”, “elem3”);
}

print getArray()[0]; // elem1
print getArray()[1]; // elem2
print getArray()[2]; // elem3

タイプヒンティングにスカラー値を追加

(原文)
Added scalar typehinting. (Ilia, Derick)

ついにつきました!
いままでは、arrayとobject(クラス)だけでしたが、intやstringも指定できるようになった。

<?php

function test1(int $sample1) {
    print $sample1;
}

function test2(string $sample2) {
    print $sample2;
}

function test3(float $sample3) {
    print $sample3;
}

test1(1); // 1
test1('hoge'); // FetalErrorが発生
test1('1'); // FetalErrorが発生

test2('foo'); // foo
test2(1); // FetalErrorが発生
test2(array('foo')); // FetalErrorが発生

test3(1.1); // 1
test3(0.0); // 0.0 (いいんだ・・・)
test3(1); // FetalErrorが発生
test3(0); // FetalErrorが発生

ちなみにエラーの場合は、以下のようなFetalErrorが出る。

PHP Catchable fatal error:  Argument 1 passed to test3() must be of the type double, integer given, called in /home/admin/install/php5.4/test_typeh.php on line 24 and defined in /home/admin/install/php5.4/test_typeh.php on line 11

Catchable fatal error: Argument 1 passed to test3() must be of the type double, integer given, called in /home/admin/install/php5.4/test_typeh.php on line 24 and defined in /home/admin/install/php5.4/test_typeh.php on line 11

Traitのサポート

(原文)
Added support for Traits. (Stefan)

Traitは多重継承ができ、Scalaで使われててアスペクト志向とかに向いているというもの。
今はextendで1つしかクラスを継承できないが、traitを使うと複数継承できる。
実際に使ってみる。

(参考)
http://wiki.php.net/rfc/traits

 <?php
 class Base {
   public function sayHello() {
     echo 'Hello ';
   }
 }
 
 trait SayWorld {
   public function sayHello() {
     parent::sayHello();
     echo 'World!';
   }
 }
 
 class MyHelloWorld extends Base {
   use SayWorld;
 }
 
 $o = new MyHelloWorld();
 $o->sayHello(); // echos Hello World!

なかなか便利そう。だがちょっと不満な点があるので、traitに関しては、別記事として書く予定。

TokyoCabinetをサポート

(原文)
Added Tokyo Cabinet abstract DB support to ext/dba. (Michael Maclean)

最近話題のKVSですね。ちょっと使ってみましょう。


1、TokyoCabinet自体をインストール

sudo yum install tokyocabinet*


2、PHPインストール

1、まずダウンロード

svn co http://svn.php.net/repository/php/php-src/trunk


2、--with-tcadbをつけて、てきとうなとこにインストール。

./buildconf
./configure --prefix=/home/admin/install/php5.4/ --with-tcadb
make
make install


3、動作テスト用スクリプトを用意

<?php

$id = dba_open("test.tch", "c", "tcadb");

if (!$id) {
    echo "dba_open failed\n";
    exit;
}

$key = "test_key";
$value = "test_value";

// 追加
dba_insert($key, $value, $id);

// 取得
var_dump(dba_fetch($key, $id));

dba_close($id);

詳しくはマニュアル参照
http://www.php.net/manual/ja/ref.dba.php


4、実行

[admin@localhost trunk]$ ~/install/php5.4/bin/php ./cabinet_test_fetch.php
string(10) "test_value"

とりあえず動くことを確認したので満足。
次。

削除されたもの

 - Removed legacy features:
  . allow_call_time_pass_reference. (Pierrick)
  . define_syslog_variables ini option and its associated function. (Kalle)
  . highlight.bg ini option. (Kalle)
  . import_request_variables(). (Kalle)
  . register_globals. (Kalle)
  . register_long_arrays ini option. (Kalle)
  . Safe mode. (Kalle)
  . Session bug compatibility mode (session.bug_compat42 and
    session.bug_compat_warn ini options). (Kalle)
  . session_is_registered(), session_register() and session_unregister()
    functions. (Kalle)
  . y2k_compliance ini option. (Kalle)

注目すべきものは、以下の2つ

ついに削除されたー。

Dtraceをサポート

(原文)
Added DTrace support. (David Soria Parra)

Dtraceは強力なデバッガで、プログラムの実行をトレースしたりします。例えばPHPでどの関数がどの関数を実行してきたのか等を調べることが出来ます。ですがSoraris用の仕組みなので今回はサンプルを用意できませんでした。。。
リンクだけ張っておきます。

Dtraceとは

Dtrace for PHP

パフォーマンス向上について

(原文)
 - Added caches to eliminate repeatable run-time bindings of functions, classes,
   constants, methods and properties (Dmitry)
 - Added a number of small performance tweaks and optimizations (Dmitry)
  . ZEND_RECV now always has IS_CV as its result
  . ZEND_CATCH now has to be used only with constant class names
  . ZEND_FETCH_DIM_? may fetch array and dimension operans in a different order
  . ZEND_RETURN is splitted into two new instructions ZEND_RETURN and
    ZEND_RETURN_BY_REF
  . optimized access to global constants using values with pre-calculated
    hash_values from litersls table
  . optimized access to static properties using executor specialization.
    A constant class name may be used as a direct operand of ZEND_FETCH_*
    instruction without previous ZEND_FETCH_CLASS.
 - Added concept of interned strings. All strings constants known at compile
  time are allocated in a single copy and never changed. (Dmitry)
 - Added an optimization which saves memory and emalloc/efree calls for empty
  HashTables (Stas, Dmitry)

まぁ色々と向上させたらしい。
PHP内部の話なのであまり興味ないあるよ。(汗

2種類のハッシュをサポート

(原文)
Added Jenkins's one-at-a-time hash support to ext/hash. (Martin Jansen)
Added FNV-1 hash support to ext/hash. (Michael Maclean)

one-at-a-timeハッシュは、perlで使われてるらしい
ぁーなんか興味ない。
次。

Reflectionクラス拡張

(原文)
Added ReflectionExtension::isTemporary() andReflectionExtension::isPersistent(). (Johannes)
Added ReflectionZendExtension class. (Johannes)
Added ReflectionClass::isCloneable(). (Felipe)
  • isTemporary()メソッド追加
  • isPersistent()メソッド追加
  • ReflectionZendExtensionクラス追加
  • isCloneable()メソッド追加

Reflectionクラスにメソッド追加? あんま興味ない。。
次。

default_charsetのデフォルトはUTF-8

(原文)
default_charset if not specified is now UTF-8 instead of ISO-8859-1. (Rasmus)

ぉぉーついにそうなりましたか。
試してみましょう。

1、--enable-mbstringつけてPHPインストール

make clean (さっきTokyoCabinetインストールしたので)
./configure --prefix=/home/admin/install/php5.4/ --enable-mbstring
make
make install

2、テスト実行

mb_internal_encoding()でちゃんとUTF8になってるか確認・・・

[admin@localhost trunk]$ ~/install/php5.4/bin/php -r "var_dump(mb_internal_encoding());"
string(10) "ISO-8859-1"

ぁ゛?

iniになんかセットされて・・・

[admin@localhost php6.0]$ ~/install/php5.4/bin/php -r "var_dump(ini_get('default_charset'));"
string(0) ""

ない。ならUTF8になるはずじゃ・・・?

[admin@localhost php6.0]$ ~/install/php5.4/bin/php -i | grep default_charset
default_charset => no value => no value

謎・・・まぁ、さすがPHPということで。

国際化に関して

国際化は、「ICU (International Components for Unicode)」というライブラリを使って実装される予定でしたが、今回のアップデートからは外されてます。先日のPHPカンファレンス2010では話題に上がりましたね。全世界に対応するとコードの量が膨大になりすぎるとかで断念したらしい。(?)
これが導入されると、mb_serlen()とか使わなくてもちゃんとマルチバイトを認識して処理してくれます。ということは英語圏の人作った膨大なスクリプトを手を加えずに再利用できるので早く導入してほしい。

まとめ

まだ他にもあるがここまで。ひたすら書きましたがそんなに変わるか?というのが印象でした。なのでPHP5.4になりそうな気はしてます。やっぱ大きいのは国際化だなー早く対応してほしい。タイプヒンティングもスカラに対応したのでプロジェクトで強制したい場合はとてもやくに立つんじゃないですかね。あと、どれくらいパフォーマンスが向上したかベンチマークしてみたいなー。