ReflectionMethod 类
(PHP 5, PHP 7, PHP 8)
简介
ReflectionMethod 类报告了一个方法的有关信息。
属性
- name
-
Method name
- class
-
Class name
预定义常量
ReflectionMethod 修饰符
ReflectionMethod::IS_STATIC
-
指示一个方法是静态(static)的。
ReflectionMethod::IS_PUBLIC
-
指示一个方法是 public 的。
ReflectionMethod::IS_PROTECTED
-
指示一个方法是 protected 的。
ReflectionMethod::IS_PRIVATE
-
指示一个方法是 private 的。
ReflectionMethod::IS_ABSTRACT
-
指示一个方法是 abstract 的。
ReflectionMethod::IS_FINAL
-
指示一个方法是 final 的。
目录
- ReflectionMethod::__construct — ReflectionMethod 的构造函数
- ReflectionMethod::export — 输出一个回调方法
- ReflectionMethod::getClosure — 返回一个动态建立的方法调用接口,译者注:可以使用这个返回值直接调用非公开方法。
- ReflectionMethod::getDeclaringClass — 获取被反射的方法所在类的反射实例
- ReflectionMethod::getModifiers — 获取方法的修饰符
- ReflectionMethod::getPrototype — 返回方法原型 (如果存在)
- ReflectionMethod::invoke — Invoke
- ReflectionMethod::invokeArgs — 带参数执行
- ReflectionMethod::isAbstract — 判断方法是否是抽象方法
- ReflectionMethod::isConstructor — 判断方法是否是构造方法
- ReflectionMethod::isDestructor — 判断方法是否是析构方法
- ReflectionMethod::isFinal — 判断方法是否定义 final
- ReflectionMethod::isPrivate — 判断方法是否是私有方法
- ReflectionMethod::isProtected — 判断方法是否是保护方法 (protected)
- ReflectionMethod::isPublic — 判断方法是否是公开方法
- ReflectionMethod::isStatic — 判断方法是否是静态方法
- ReflectionMethod::setAccessible — 设置方法是否访问
- ReflectionMethod::__toString — 返回反射方法对象的字符串表达

User Contributed Notes 3 notes
Anonymous ¶
1 year ago
We can make a "Automatic dependenci injector" in classes when her constructors depends other classes (with type hint).
<?php
class Dependence1 {
function foo() {
echo "foo";
}
}
class Dependence2 {
function foo2() {
echo "foo2";
}
}
final class myClass
{
private $dep1;
private $dep2;
public function __construct(
Dependence1 $dependence1,
Dependence2 $dependence2
)
{
$this->dep1 = $dependence1;
$this->dep2 = $dependence2;
}
}
// Automatic dependence injection (CLASSES)
$constructor = new ReflectionMethod(myClass::class, '__construct');
$parameters = $constructor->getParameters();
$dependences = [];
foreach ($parameters as $parameter) {
$dependenceClass = (string) $parameter->getType();
$dependences[] = new $dependenceClass();
}
$instance = new myClass(...$dependences);
var_dump($instance);
?>
Results in:
object(myClass)#6 (2) {
["dep1":"myClass":private]=>
object(Dependence1)#4 (0) {
}
["dep2":"myClass":private]=>
object(Dependence2)#5 (0) {
}
}
webseiten dot designer at googlemail dot com ¶
11 years ago
Note that the public member $class contains the name of the class in which the method has been defined:
<?php
class A {public function __construct() {}}
class B extends A {}
$method = new ReflectionMethod('B', '__construct');
echo $method->class; // prints 'A'
?>
no dot prob at gmx dot net ¶
16 years ago
I have written a function which returns the value of a given DocComment tag.
Full example:
<?php
header('Content-Type: text/plain');
class Example
{
/**
* This is my DocComment!
*
* @DocTag: prints Hello World!
*/
public function myMethod()
{
echo 'Hello World!';
}
}
function getDocComment($str, $tag = '')
{
if (empty($tag))
{
return $str;
}
$matches = array();
preg_match("/".$tag.":(.*)(\\r\\n|\\r|\\n)/U", $str, $matches);
if (isset($matches[1]))
{
return trim($matches[1]);
}
return '';
}
$method = new ReflectionMethod('Example', 'myMethod');
// will return Hello World!
echo getDocComment($method->getDocComment(), '@DocTag');
?>
Maybe you can add this functionality to the getDocComment methods of the reflection classes.
备份地址:http://www.lvesu.com/blog/php/class.reflectionmethod.php