LaravelConf Taiwan 2019 announcement

ReflectionMethod 类

(PHP 5, PHP 7)

简介

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 export ( string $class , string $name [, bool $return = false ] ) : string
public getClosure ( object $object ) : Closure
public getModifiers ( void ) : int
public getPrototype ( void ) : ReflectionMethod
public invoke ( object $object [, mixed $parameter [, mixed $... ]] ) : mixed
public invokeArgs ( object $object , array $args ) : mixed
public isAbstract ( void ) : bool
public isConstructor ( void ) : bool
public isDestructor ( void ) : bool
public isFinal ( void ) : bool
public isPrivate ( void ) : bool
public isProtected ( void ) : bool
public isPublic ( void ) : bool
public isStatic ( void ) : bool
public setAccessible ( bool $accessible ) : void
public __toString ( void ) : string
/* 继承的方法 */
final private ReflectionFunctionAbstract::__clone ( void ) : void
public ReflectionFunctionAbstract::getFileName ( void ) : string
public ReflectionFunctionAbstract::getName ( void ) : string
abstract public ReflectionFunctionAbstract::__toString ( void ) : 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

add a note add a note

User Contributed Notes 2 notes

up
7
webseiten dot designer at googlemail dot com
8 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'
?>
up
-11
no dot prob at gmx dot net
13 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.
To Top