Do You PHP はてブロ

Do You PHPはてなからはてブロに移動しました

Scalar Type Hints are Here!

ずいぶん前にもパッチについてのエントリを書きましたが、ついにphp-srcのtrunkにマージされた模様です。


About an hour ago, something I've been fighting for almost 2 years happened. The Scalar Type Hinting patch for PHP (the one I wrote almost a year ago) has been adjusted for PHP's trunk tree and committed by Derick.

早速、trunk版(PHP-5.3.99-dev)をインストールして試してみました。

$ php -v
PHP 5.3.99-dev (cli) (built: May 27 2010 13:18:02)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2010 Zend Technologies
$ 
$ cat type_hinting.php
<?php
function test(string $a, bool $b = NULL, integer $c, double $d = NULL, $e) {
    print "ok\n";
}

test('foo', NULL, 1, NULL, 'bar');
test('foo', true, 0, -1., NULL);
test('true', false, -1, 2.2222, 1.1);
test('1.1', false, -1, 11111111111111111111111111, true);
test('1', false, -1, NULL, new stdClass);
$ 
$ php type_hinting.php
ok
ok
ok
ok
ok
$ 

おお。。。これですよ、これ。Type Hintingを指定しなければ何でもOK(第5引数に注目)なので、php.iniで有効/無効を切り替えるとか面倒なことはいらないと思うなぁ。使いたくないなら、指定しなきゃいいだけなので。
ただし、『32bitと64bitでintegerの限界値違うけどどうなの?』とか現在もいろいろと議論されているようで、最終的にどうなるか。。。