ReflectionClass::getMethods
(PHP 5, PHP 7, PHP 8)
ReflectionClass::getMethods — Gets an array of methods
Beschreibung
$filter
= null
): arrayGets an array of methods for the class.
Parameter-Liste
-
filter
-
Filter the results to include only methods with certain attributes. Defaults to no filtering.
Any bitwise disjunction of
ReflectionMethod::IS_STATIC
,ReflectionMethod::IS_PUBLIC
,ReflectionMethod::IS_PROTECTED
,ReflectionMethod::IS_PRIVATE
,ReflectionMethod::IS_ABSTRACT
,ReflectionMethod::IS_FINAL
, so that all methods with any of the given attributes will be returned.Hinweis: Note that other bitwise operations, for instance
~
will not work as expected. In other words, it is not possible to retrieve all non-static methods, for example.
Rückgabewerte
An array of ReflectionMethod objects reflecting each method.
Changelog
Version | Beschreibung |
---|---|
7.2.0 |
filter is nullable now.
|
Beispiele
Beispiel #1 Basic usage of ReflectionClass::getMethods()
<?php
class Apple {
public function firstMethod() { }
final protected function secondMethod() { }
private static function thirdMethod() { }
}
$class = new ReflectionClass('Apple');
$methods = $class->getMethods();
var_dump($methods);
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
array(3) { [0]=> object(ReflectionMethod)#2 (2) { ["name"]=> string(11) "firstMethod" ["class"]=> string(5) "Apple" } [1]=> object(ReflectionMethod)#3 (2) { ["name"]=> string(12) "secondMethod" ["class"]=> string(5) "Apple" } [2]=> object(ReflectionMethod)#4 (2) { ["name"]=> string(11) "thirdMethod" ["class"]=> string(5) "Apple" } }
Beispiel #2 Filtering results from ReflectionClass::getMethods()
<?php
class Apple {
public function firstMethod() { }
final protected function secondMethod() { }
private static function thirdMethod() { }
}
$class = new ReflectionClass('Apple');
$methods = $class->getMethods(ReflectionMethod::IS_STATIC | ReflectionMethod::IS_FINAL);
var_dump($methods);
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
array(2) { [0]=> object(ReflectionMethod)#2 (2) { ["name"]=> string(12) "secondMethod" ["class"]=> string(5) "Apple" } [1]=> object(ReflectionMethod)#3 (2) { ["name"]=> string(11) "thirdMethod" ["class"]=> string(5) "Apple" } }
Siehe auch
- ReflectionClass::getMethod() - Gets a ReflectionMethod for a class method
- get_class_methods() - Ermittelt die Namen der definierten Methoden einer Klasse