Do You PHP はてブロ

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

Type hinting/castingパッチはPHP5.3.0でも動く

前のエントリで


PHP5.3.0に上のパッチを当てることもできるようですが、タイプキャストの方しか動作しませんでした。。。残念。。。

とか書いたんですが、モーレツに恥ずかしいことにre2cが入ってませんでした。。。><

$ ./configure --with-apxs2=/usr/sbin/apxs --prefix=/usr/local/lib/php53 --with-pear=/usr/local/lib/php53/pear --with-config-file-path=/usr/local/lib/php53/ini/5.3.0 --with-config-file-scan-dir=/usr/local/lib/php53/ini.d --enable-zend-multibyte --enable-mbstring --enable-dom --enable-filter=shared --with-gettext=shared --with-mcrypt=shared --with-gd=shared --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --enable-soap=shared --enable-pdo=shared --with-pdo-sqlite=shared --with-sqlite=shared --enable-sqlite-utf8 --with-openssl=shared --with-curl=shared --with-curlwrappers --enable-bcmath=shared --enable-pcntl=shared --enable-sockets=shared --with-xsl=shared --enable-zip=shared --with-mysql=shared --with-pdo-mysql=shared --with-pdo-pgsql=shared --with-pgsql=shared --with-bz2=shared --enable-calendar=shared --enable-exif=shared --with-mhash=shared --enable-intl=shared --with-libxml-dir --enable-sysvmsg=shared --enable-sysvsem=shared --enable-sysvshm=shared --with-tidy=shared --enable-wddx=shared --with-readline | tee test.log
checking for egrep... grep -E
checking for a sed that does not truncate output... /bin/sed
             :
checking for bison... bison -y
checking for bison version... 2.3 (ok)
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking whether to enable computed goto gcc extension with re2c... no

とりあえず、

$ sudo yum install -y re2c

でインストールし、Zend/zend_language_scanner.cを削除して再度configure/make/make install。テストを実行すると、全てパスしました。

$ PATH=/usr/local/lib/php53/bin:$PATH pear run-tests
Running 23 tests
PASS [ 1/23] Parameter type casting - Testing integer hint[param_type_cast_001.phpt]
PASS [ 2/23] Parameter type casting - Testing string hint[param_type_cast_002.phpt]
PASS [ 3/23] Parameter type casting - Testing array hint[param_type_cast_003.phpt]
PASS [ 4/23] Parameter type casting - Testing float hint[param_type_cast_004.phpt]
PASS [ 5/23] Parameter type casting - Testing boolean hint[param_type_cast_005.phpt]
PASS [ 6/23] Parameter type casting - default parameter handling[param_type_cast_006.phpt]
PASS [ 7/23] Parameter cast hint - Reflection[param_type_cast_007.phpt]
PASS [ 8/23] Parameter type hint - Testing integer hint[param_type_hint_001.phpt]
PASS [ 9/23] Parameter type hint - Reflection[param_type_hint_002.phpt]
PASS [10/23] Parameter type hint - Wrong parameter for integer[param_type_hint_003.phpt]
PASS [11/23] Parameter type hint - Wrong parameter for double[param_type_hint_004.phpt]
PASS [12/23] Parameter type hint - Wrong parameter for string[param_type_hint_005.phpt]
PASS [13/23] Parameter type hint - Testing NULL when allowed[param_type_hint_006.phpt]
PASS [14/23] Parameter type hint - Testing parameter with reference[param_type_hint_007.phpt]
PASS [15/23] Parameter type hint - Testing string type hint with reference[param_type_hint_008.phpt]
PASS [16/23] Parameter type hint - Testing default parameter value[param_type_hint_009.phpt]
PASS [17/23] Parameter type hint - Testing with heredoc[param_type_hint_010.phpt]
PASS [18/23] Parameter type hint - Testing with undefined variable[param_type_hint_011.phpt]
PASS [19/23] Parameter type hint - Testing with lambda function[param_type_hint_012.phpt]
PASS [20/23] Parameter type hint - Testing with lambda function using 2 parameters[param_type_hint_013.phpt]
PASS [21/23] Parameter type hint - Testing in function inside function[param_type_hint_014.phpt]
PASS [22/23] Parameter type hint - Testing with call_user_func()[param_type_hint_015.phpt]
PASS [23/23] Parameter type hint - Testing default parameter values[param_type_hint_016.phpt]
TOTAL TIME: 00:02
23 PASSED TESTS
0 SKIPPED TESTS
$ 

ついでに、以下のようなコードでも動作確認。

$ cat test.php
<?php
class test
{
    public function hoge(int $a) {
        var_dump($a);
    }
}
$obj = new test();
$obj->hoge(1);
$obj->hoge(PHP_INT_MAX);

/**
 * Catchable fatal error: Argument 1 passed to test::hoge() must be of the type integer, double given
 */
//$obj->hoge(PHP_INT_MAX + 1);

/**
 * Catchable fatal error: Argument 1 passed to test::hoge() must be of the type integer, string given
 */
//$obj->hoge('1');
$ /usr/local/lib/php53/bin/php -n test.php
int(1)
int(2147483647)
$ 

あー。早くこれ欲しいなぁ。。。