PHP 8.3.4 Released!

ReflectionFunction::__construct

(PHP 5, PHP 7, PHP 8)

ReflectionFunction::__constructReflectionFunction オブジェクトを作成する

説明

public ReflectionFunction::__construct(Closure|string $function)

ReflectionFunction オブジェクトを作成します。

パラメータ

function

調べたい関数あるいはクロージャの名前。

エラー / 例外

function パラメータが正しい関数名でない場合に ReflectionException が発生します。

例1 ReflectionFunction::__construct() の例

<?php
/**
* 簡単なカウンタ
*
* @return int
*/
function counter1()
{
static
$c = 0;
return ++
$c;
}

/**
* 別の簡単なカウンタ
*
* @return int
*/
$counter2 = function()
{
static
$d = 0;
return ++
$d;

};

function
dumpReflectionFunction($func)
{
// 基本情報を表示します
printf(
"\n\n===> The %s function '%s'\n".
" declared in %s\n".
" lines %d to %d\n",
$func->isInternal() ? 'internal' : 'user-defined',
$func->getName(),
$func->getFileName(),
$func->getStartLine(),
$func->getEndline()
);

// ドキュメントコメントを表示します
printf("---> Documentation:\n %s\n", var_export($func->getDocComment(), 1));

// static変数が存在すれば表示します
if ($statics = $func->getStaticVariables())
{
printf("---> Static variables: %s\n", var_export($statics, 1));
}
}

// ReflectionFunction クラスのインスタンスを作成します
dumpReflectionFunction(new ReflectionFunction('counter1'));
dumpReflectionFunction(new ReflectionFunction($counter2));
?>

上の例の出力は、 たとえば以下のようになります。

===> The user-defined function 'counter1'
     declared in Z:\reflectcounter.php
     lines 7 to 11
---> Documentation:
 '/**
 * 簡単なカウンタ
 *
 * @return    int
 */'
---> Static variables: array (
  'c' => 0,
)


===> The user-defined function '{closure}'
     declared in Z:\reflectcounter.php
     lines 18 to 23
---> Documentation:
 '/**
 * 別の簡単なカウンタ
 *
 * @return    int
 */'
---> Static variables: array (
  'd' => 0,
)

参考

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top