リフレクションのgetDocComment試してみた

PHPリフレクションで実装されているgetDocCommentメソッドを試してみた。PHP 5.1.0以降の環境で利用できるはず。

<?php
class Foo
{
    /**
     * the value
     * @var string
     */
    public $bar;

    /**
     * @param string $bar the value to compare
     * @return boolean if equals or not
     */
    public function baz($bar)
    {
        return $bar === $this->bar;
    }
}

$refClass = new ReflectionClass('Foo');

$methods = $refClass->getMethods();
foreach ($methods as $method) {
    echo $method->getDocComment(), "\n";
}

$properties = $refClass->getProperties();
foreach ($properties as $property) {
    echo $property->getDocComment(), "\n";
}

というコードを実行すると次のような結果が返ってくる。

/**
     * @param string $bar the value to compare
     * @return boolean if equals or not
     */
/**
     * the value
     * @var string
     */

この文字列を解析してうまく使うとJavaアノテーションっぽくメタデータの取り出しができるかも。というよりS2Container.PHP5は確かこの辺を使ってたような。