Php/docs/reflectionfunction.construct

提供:Dev Guides
< Php
移動先:案内検索

ReflectionFunction::__construct

(PHP 5, PHP 7)

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


説明

public ReflectionFunction::__construct ( mixed $name )

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


パラメータ

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


返り値

値を返しません。


エラー / 例外

name パラメータが正しい関数名でない場合に 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));    // 静的変数が存在すれば表示します    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,
)

参考