文字

ReflectionMethod 类

(PHP 5)

简介

ReflectionMethod 类报告了一个方法的有关信息。

类摘要

ReflectionMethod extends ReflectionFunctionAbstract implements Reflector {
const integer IS_STATIC = 1 ;
const integer IS_PUBLIC = 256 ;
const integer IS_PROTECTED = 512 ;
const integer IS_PRIVATE = 1024 ;
const integer IS_ABSTRACT = 2 ;
const integer IS_FINAL = 4 ;
public $name ;
public $class ;
public __construct ( mixed $class , string $name )
public static string export ( string $class , string $name [, bool $return = false ] )
public Closure getClosure ( object $object )
public ReflectionClass getDeclaringClass ( void )
public int getModifiers ( void )
public ReflectionMethod getPrototype ( void )
public mixed invoke ( object $object [, mixed $parameter [, mixed $... ]] )
public mixed invokeArgs ( object $object , array $args )
public bool isAbstract ( void )
public bool isConstructor ( void )
public bool isDestructor ( void )
public bool isFinal ( void )
public bool isPrivate ( void )
public bool isProtected ( void )
public bool isPublic ( void )
public bool isStatic ( void )
public void setAccessible ( bool $accessible )
public string __toString ( void )
final private void ReflectionFunctionAbstract::__clone ( void )
public ReflectionClass ReflectionFunctionAbstract::getClosureScopeClass ( void )
public object ReflectionFunctionAbstract::getClosureThis ( void )
public string ReflectionFunctionAbstract::getDocComment ( void )
public int ReflectionFunctionAbstract::getEndLine ( void )
public ReflectionExtension ReflectionFunctionAbstract::getExtension ( void )
public string ReflectionFunctionAbstract::getExtensionName ( void )
public string ReflectionFunctionAbstract::getFileName ( void )
public string ReflectionFunctionAbstract::getName ( void )
public string ReflectionFunctionAbstract::getNamespaceName ( void )
public int ReflectionFunctionAbstract::getNumberOfParameters ( void )
public int ReflectionFunctionAbstract::getNumberOfRequiredParameters ( void )
public array ReflectionFunctionAbstract::getParameters ( void )
public ReflectionType ReflectionFunctionAbstract::getReturnType ( void )
public string ReflectionFunctionAbstract::getShortName ( void )
public int ReflectionFunctionAbstract::getStartLine ( void )
public array ReflectionFunctionAbstract::getStaticVariables ( void )
public bool ReflectionFunctionAbstract::hasReturnType ( void )
public bool ReflectionFunctionAbstract::inNamespace ( void )
public bool ReflectionFunctionAbstract::isClosure ( void )
public bool ReflectionFunctionAbstract::isDeprecated ( void )
public bool ReflectionFunctionAbstract::isGenerator ( void )
public bool ReflectionFunctionAbstract::isInternal ( void )
public bool ReflectionFunctionAbstract::isUserDefined ( void )
public bool ReflectionFunctionAbstract::isVariadic ( void )
public bool ReflectionFunctionAbstract::returnsReference ( void )
abstract public void ReflectionFunctionAbstract::__toString ( void )
}

属性

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 的。

Table of Contents

  • 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 — 返回反射方法对象的字符串表达

用户评论:

[#1] webseiten dot designer at googlemail dot com [2011-04-02 11:53:12]

Note that the public member $class contains the name of the class in which the method has been defined:

<?php
class {public function __construct() {}}
class 
extends {}

$method = new ReflectionMethod('B''__construct');
echo 
$method->class// prints 'A'
?>

[#2] no dot prob at gmx dot net [2006-06-02 01:09:12]

I have written a function which returns the value of a given DocComment tag.

Full example:

<?php

header
('Content-Type: text/plain');

class 
Example
{
    

    
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.

上一篇: 下一篇: