Reflection
在线手册:中文 英文
PHP手册

The ReflectionFunction class

(PHP 5)

简介

The ReflectionFunction class reports information about a function.

类摘要

ReflectionFunction extends ReflectionFunctionAbstract implements Reflector {
/* 常量 */
const integer IS_DEPRECATED = 262144 ;
/* 属性 */
public $name ;
/* 方法 */
public __construct ( mixed $name )
public static string export ( string $name [, string $return ] )
public Closure getClosure ( void )
public mixed invoke ([ mixed $parameter [, mixed $... ]] )
public mixed invokeArgs ( array $args )
public bool isDisabled ( void )
public string __toString ( void )
/* 继承的方法 */
final private void ReflectionFunctionAbstract::__clone ( void )
public ReflectionExtension ReflectionFunctionAbstract::getExtension ( void )
public string ReflectionFunctionAbstract::getName ( void )
abstract public void ReflectionFunctionAbstract::__toString ( void )
}

属性

name

Name of the function. Read-only, throws ReflectionException in attempt to write.

预定义常量

ReflectionFunction Modifiers

ReflectionFunction::IS_DEPRECATED

Indicates deprecated functions.

Table of Contents


Reflection
在线手册:中文 英文
PHP手册
PHP手册 - N: The ReflectionFunction class

用户评论:

Lorenz R.S. (12-Aug-2011 09:24)

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 ]

uramihsayibok, gmail, com (24-Oct-2010 11: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);
}

?>