PHP: Hypertext Preprocessor (original) (raw)

ReflectionClass::getMethod

(PHP 5, PHP 7, PHP 8)

ReflectionClass::getMethod — Gets a ReflectionMethod for a class method

Description

Parameters

name

The method name to reflect.

Examples

Example #1 Basic usage of ReflectionClass::getMethod()

<?php $class = new ReflectionClass('ReflectionClass'); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">method = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span><span class="mord mathnormal">o</span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>class->getMethod('getMethod'); var_dump($method); ?>

The above example will output:

object(ReflectionMethod)#2 (2) { ["name"]=> string(9) "getMethod" ["class"]=> string(15) "ReflectionClass" }

Found A Problem?

Jarrod Nettles

14 years ago

`If you ever need to get the type hint of a parameter in a method use this.

parameters=parameters = parameters=reflector->getMethod('FireCannon')->getParameters();//Loop through each parameter and get the type foreach($parameters as $param) { //Before you call getClass() that class must be defined! echo $param->getClass()->name; }?>

`

sagittaracc at gmail dot com

3 years ago

`if you ever need to get the body of a method, use this extension (https://github.com/sagittaracc/reflection):

namespace sagittaracc\classes;

class Test
{
public function method()
{
if (true) {
return 'this method';
}

return 'never goes here';
}
}

$reflection = new ReflectionClass(Test::class); method=method = method=reflection->getMethod('method');
echo $method->body; // if (true) { return 'this method'; } return 'never goes here';

`