Here is a concise example of ReflectionFunction usage for Parameter Reflection / introspection (e.g. to automatically generate API descriptions)
<?php
$properties = $reflector->getProperties();
$refFunc = new ReflectionFunction('preg_replace');
foreach( $refFunc->getParameters() as $param ){
//invokes ■ReflectionParameter::__toString
print $param;
}
?>
prints:
Parameter #0 [ <required> $regex ]
Parameter #1 [ <required> $replace ]
Parameter #2 [ <required> $subject ]
Parameter #3 [ <optional> $limit ]
Parameter #4 [ <optional> &$count ]
ReflectionFunction クラス
(PHP 5)
導入
ReflectionFunction クラスは 関数についての情報を報告します。
クラス概要
プロパティ
- name
-
関数名。読み込み専用で、書き込もうとすると ReflectionException をスローします。
定義済み定数
ReflectionFunction の修飾子
-
ReflectionFunction::IS_DEPRECATED -
非推奨の関数であることを示します。
目次
- ReflectionFunction::__construct — ReflectionFunction オブジェクトを作成する
- ReflectionFunction::export — 関数をエクスポートする
- ReflectionFunction::getClosure — この関数に動的に作成されたクロージャを返す
- ReflectionFunction::invoke — 関数を起動する
- ReflectionFunction::invokeArgs — 引数を指定して関数を起動する
- ReflectionFunction::isDisabled — 関数が無効になっているかどうかを調べる
- ReflectionFunction::__toString — 文字列に変換する
Lorenz R.S.
12-Aug-2011 01:24
uramihsayibok, gmail, com
24-Oct-2010 03:00
ReflectionFunction will not work on class methods - instance or static. That is,
<?php
class A {
function B() {}
static function C() {}
}
new ReflectionFunction("A::B"); // throws "does not exist" ReflectionException
new ReflectionFunction("A::C"); // ditto
?>
The array syntax for method callbacks does not work either but throws a warning instead (__construct wants a string, not an array).
Since I don't know ahead of time whether something is a function or a class method, I have this:
<?php
function ReflectionFunctionFactory($callback) {
if (is_array($callback)) {
// must be a class method
list($class, $method) = $callback;
return new ReflectionMethod($class, $method);
}
// class::method syntax
if (is_string($callback) && strpos($callback, "::") !== false) {
list($class, $method) = explode("::", $callback);
return new ReflectionMethod($class, $method);
}
// objects as functions (PHP 5.3+)
if (version_compare(PHP_VERSION, "5.3.0", ">=") && method_exists($callback, "__invoke")) {
return new ReflectionMethod($callback, "__invoke");
}
// assume it's a function
return new ReflectionFunction($callback);
}
?>
