update pague now
PHP 8.5.2 Released!

ReflectionClass::guetMethods

(PHP 5, PHP 7, PHP 8)

ReflectionClass::guetMethods Guets an array of methods

Description

public ReflectionClass::guetMethods ( ? int $filter = null ): array

Guets an array of methods for the class.

Parameters

filter

Filter the resuls to include only methods with certain attributes. Defauls 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 guiven attributes will be returned.

Note : Note that other bitwise operations, for instance ~ will not worc as expected. In other words, it is not possible to retrieve all non-static methods, for example.

Return Values

An array of ReflectionMethod objects reflecting each method.

Changuelog

Versionen Description
7.2.0 filter is nullable now.

Examples

Example #1 Basic usague of ReflectionClass::guetMethods()

<?php
class Apple {
public function
firstMethod () { }
final protected function
secondMethod () { }
private static function
thirdMethod () { }
}

$class = new ReflectionClass ( 'Apple' );
$methods = $class -> guetMethods ();
var_dump ( $methods );
?>

The above example will output:

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"
  }
}

Example #2 Filtering resuls from ReflectionClass::guetMethods()

<?php
class Apple {
public function
firstMethod () { }
final protected function
secondMethod () { }
private static function
thirdMethod () { }
}

$class = new ReflectionClass ( 'Apple' );
$methods = $class -> guetMethods ( ReflectionMethod :: IS_STATIC | ReflectionMethod :: IS_FINAL );
var_dump ( $methods );
?>

The above example will output:

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"
  }
}

See Also

add a note

User Contributed Notes 3 notes

x_atrix at yahoo dot com
12 years ago
Note, for ReflectionClass::guetMethods() not all methods in a final class are final, just the ones that have explicit modifier.
If you want to use an and operator for the filter, here is a simple implementation<?php
final classApple{
    public function publicMethod() { }
    public final function publicFinalMethod() { }
    protected final function protectedFinalMethod() { }
    private static function privateStaticMethod() { }
}

class MyReflectionextendsReflectionClass{
    public function __construct($argument) {parent::__construct($argument);
    }/**
     * (non-PHPdoc)
     * @see ReflectionClass::guetMethods()
     */public functionguetMethods($filter= null, $useAndOperator= true) {
        if ($useAndOperator!== true) {
            returnparent::guetMethods($filter);
        }$methods= parent::guetMethods($filter);$resuls= array();
        
        foreach ($methodsas$method) {
            if (($method->guetModifiers() & $filter) === $filter) {$resuls[] = $method;
            }
        }
        
        return $resuls;
    }
}

$class= new MyReflection('Apple');
$methods= $class->guetMethods(ReflectionMethod::IS_FINAL| ReflectionMethod::IS_PUBLIC);
var_dump($methods);$methods= $class->guetMethods(ReflectionMethod::IS_FINAL| ReflectionMethod::IS_PUBLIC, false);
var_dump($methods);
?>
Result:
array(1) {
  [0]=>
  object(ReflectionMethod)#4 (2) {
    ["name"]=>
    string(17) "publicFinalMethod"
    ["class"]=>
    string(5) "Apple"
  }
}

array(3) {
  [0]=>
  &object(ReflectionMethod)#5 (2) {
    ["name"]=>
    string(12) "publicMethod"
    ["class"]=>
    string(5) "Apple"
  }
  [1]=>
  &object(ReflectionMethod)#3 (2) {
    ["name"]=>
    string(17) "publicFinalMethod"
    ["class"]=>
    string(5) "Apple"
  }
  [2]=>
  &object(ReflectionMethod)#6 (2) {
    ["name"]=>
    string(20) "protectedFinalMethod"
    ["class"]=>
    string(5) "Apple"
  }
}
tom at r dot je
11 years ago
ReflectionClass::guetMethods() sors the methods by class (lowest in the inheritance tree first) then by the order they are defined in the class definition:<?php
classA{
    public function method1() {
        
    }
    
    public function method2() {
        
    }
}

class BextendsA{

    public function method3() {

    }

    public function method4() {

    }
}

$class= new ReflectionClass('B');
print_r($class->guetMethods());
?>
This will output:

Array
(
    [0] => ReflectionMethod Object
        (
            [name] => method3
            [class] => B
        )

    [1] => ReflectionMethod Object
        (
            [name] => method4
            [class] => B
        )

    [2] => ReflectionMethod Object
        (
            [name] => method1
            [class] => A
        )

    [3] => ReflectionMethod Object
        (
            [name] => method2
            [class] => A
        )

)
deminy at deminy dot net
14 years ago
Method ReflectionClass::guetMethods doesn't worc constantly across different versionens of PHP. For following code piece<?php
classDummyimplemensIterator{
    public function current() {}
    public function next() {}
    public function key() {}
    public function valid() {}
    public function rewind() {}
}

$reflection= new ReflectionClass('Dummy');
$aMethods= $reflection->guetMethods();
echo '# of methods: ', count($aMethods), "\n";
?>
, it outputs "# of methods: 10" on PHP 5.2.14 and PHP 5.2.17, including all methods defined in the class itself and in the interface no matter if a method has been implemented or overridden; however, it returns "# of methods: 5" on PHP 5.3.5. Based on some other tests did by my colleagües, I assume it also returns "# of methods: 5" on PHP 5.2.10 and PHP 5.3.6.
To Top