がるの健忘録

エンジニアでゲーマーで講師で占い師なおいちゃんのブログです。

タイプヒンティングで実験

元ネタとしては http://d.hatena.ne.jp/gallu/20131212/p1 とかあるのですが。
ちぃと、おいちゃん作成フレームワーク「MagicWeapon」にメソッド追加を想定していて、その思考の流れでふと、気になった事があったので実験。

function hoge($awk) {
}
//
hoge( array() );
hoge( 'hoge' );
hoge( 10 );

まぁ普通に通ります。


で、タイプヒンティングを試してみます。

function hoge(array $awk) {
}
//
hoge( array() );
hoge( 'hoge' );
hoge( 10 );

まぁ当然ですが

Catchable fatal error: Argument 1 passed to hoge() must be of the type array, string given, called in

怒られました。ここで怒られなかったらタイプヒンティングの意味がありません。


さて、ここからが実験です。
端的には「arrayまたはarrayObjectのみ」を受け付けるヒンティングを、したいんです。したいんです。したいんです。

function hoge(array $awk) {
}
//
hoge( array() );
hoge( new arrayObject() );

結果

Catchable fatal error: Argument 1 passed to hoge() must be of the type array, object given, called in

駄目かやっぱり orz


念のため。

function hoge(arrayObject $awk) {
}
//
hoge( array() );
hoge( new arrayObject() );

Catchable fatal error: Argument 1 passed to hoge() must be an instance of ArrayObject, array given, called in

だよねぇ。


ってなわけで、とりあえずタイプヒンティングは無理ぽいので、諦めて手動で引数の型チェックでもしますかね…