ReflectionClass::getMethods

(PHP 5)

ReflectionClass::getMethodsدریافت فهرست متدها

Description

public array ReflectionClass::getMethods ([ string $filter ] )

دریافت فهرست متدها.

Warning

This function is currently not documented; only its argument list is available.

Parameters

filter

ترکیبی از ReflectionMethod::IS_STATIC، ReflectionMethod::IS_PUBLIC، ReflectionMethod::IS_PROTECTED، ReflectionMethod::IS_PRIVATE، ReflectionMethod::IS_ABSTRACT، ReflectionMethod::IS_FINAL.

Return Values

array متدها.

See Also

add a note add a note

User Contributed Notes 3 notes

up
5
erik at dubbelboer dot com
2 years ago
The $filter uses an OR to filter the methods.

So php ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC will return all methods which are static or public methods, NOT only the methods which are both static and private.
up
1
deminy at deminy dot net
2 years ago
Method ReflectionClass::getMethods doesn't work constantly across different versions of PHP. For following code piece

<?php
class Dummy implements Iterator
{
    public function
current () {}
    public function
next () {}
    public function
key () {}
    public function
valid () {}
    public function
rewind () {}
}

$reflection = new ReflectionClass('Dummy');
$aMethods = $reflection->getMethods();
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 colleagues, I assume it also returns "# of methods: 5" on PHP 5.2.10 and PHP 5.3.6.
up
0
x_atrix at yahoo dot com
9 months ago
Note, for ReflectionClass::getMethods() 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 class Apple {
    public function
publicMethod() { }
    public final function
publicFinalMethod() { }
    protected final function
protectedFinalMethod() { }
    private static function
privateStaticMethod() { }
}

class
MyReflection extends ReflectionClass {
    public function
__construct($argument) {
       
parent::__construct($argument);
    }
   
   
/**
     * (non-PHPdoc)
     * @see ReflectionClass::getMethods()
     */
   
public function getMethods($filter = null, $useAndOperator = true) {
        if (
$useAndOperator !== true) {
            return
parent::getMethods($filter);
        }
       
       
$methods = parent::getMethods($filter);
       
$results = array();
       
        foreach (
$methods as $method) {
            if ((
$method->getModifiers() & $filter) === $filter) {
               
$results[] = $method;
            }
        }
       
        return
$results;
    }
}

$class = new MyReflection('Apple');
$methods = $class->getMethods(ReflectionMethod::IS_FINAL | ReflectionMethod::IS_PUBLIC);
var_dump($methods);

$methods = $class->getMethods(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"
  }
}
To Top