PHP Reflectionの話

記事紹介
Using the PHP Reflection API for fun and profit


PHP5から存在するReflectionClassというのがあって、これを使うとメソッド名とか、PHPdocとか、メソッドの引数とか、色々なクラス内の情報を取得できる。

http://api.cakephp.org のAPIマニュアルも、API Generatorプラグインを使って表示してると思うんだけど、ここでもReflectionClassを多様して動的にAPIドキュメントを生成している。

例えば、

$modelReflector = new ReflectionClass('Model');
$method = $modelReflector->getMethod('find');
$parameters = $method->getParameters();
$doc = $method->getDocComment();

とかやれば、$parametersにModelクラスのFindメソッドのパラメータリストが入るし、$docにはphp docのコメント情報が入る。


API Generatorプラグインが動的にAPI情報画面を生成するっていうのをCakePHP勉強会@福岡で聞いたときは、どうやってるんだろと思ったけど、こいうのを使ってる訳ね。


追記
ちゃんとyandodさんのCakePHP福岡の発表資料にReflectionって書いてあった。
http://d.hatena.ne.jp/yandod/20090316/1237218756