note 112659 added to reflectionclass.getmethods
| From: | x_atrix at yahoo dot com | Date: | Wed, 10 Jul 2013 05:00:29 +0000 |
| Subject: | note 112659 added to reflectionclass.getmethods | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195553@lists.php.net to get a copy of this message | ||
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"
}
}
----
Server IP: 80.64.47.13
Probable Submitter: 87.219.185.23
----
Manual Page -- http://php.net/manual/en/reflectionclass.getmethods.php
Edit -- https://master.php.net/note/edit/112659
Del: integrated -- https://master.php.net/note/delete/112659/integrated
Del: useless -- https://master.php.net/note/delete/112659/useless
Del: bad code -- https://master.php.net/note/delete/112659/bad+code
Del: spam -- https://master.php.net/note/delete/112659/spam
Del: non-english -- https://master.php.net/note/delete/112659/non-english
Del: in docs -- https://master.php.net/note/delete/112659/in+docs
Del: other reasons-- https://master.php.net/note/delete/112659
Reject -- https://master.php.net/note/reject/112659
Search -- https://master.php.net/manage/user-notes.php