略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: ReflectionMethod

2024-11-13

ReflectionMethod 类

(PHP 5, PHP 7, PHP 8)

简介

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

类摘要

class 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(): int
public invoke(object $object, mixed $parameter = ?, mixed $... = ?): mixed
public invokeArgs(object $object, array $args): mixed
public isAbstract(): bool
public isConstructor(): bool
public isDestructor(): bool
public isFinal(): bool
public isPrivate(): bool
public isProtected(): bool
public isPublic(): bool
public isStatic(): bool
public setAccessible(bool $accessible): void
public __toString(): string
/* 继承的方法 */
public ReflectionFunctionAbstract::getAttributes(?string $name = null, int $flags = 0): array
}

属性

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

目录

add a noteadd a note

User Contributed Notes 3 notes

up
7
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) {
  }
}
up
11
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'
?>
up
-16
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.

官方地址:https://www.php.net/manual/en/class.reflectionmethod.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3