note 100570 added to class.reflectionfunction
| From: | uramihsayibok at osu1 dot php dot net | Date: | Sun, 24 Oct 2010 10:00:51 +0000 |
| Subject: | note 100570 added to class.reflectionfunction | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173412@lists.php.net to get a copy of this message | ||
ReflectionFunction will not work on class methods - instance or static. That is,
<?php
class A {
function B() {}
static function C() {}
}
new ReflectionFunction("A::B"); // throws "does not exist" ReflectionException
new ReflectionFunction("A::C"); // ditto
?>
The array syntax for method callbacks does not work either but throws a warning instead (__construct
wants a string, not an array).
Since I don't know ahead of time whether something is a function or a class method, I have
this:
<?php
function ReflectionFunctionFactory($callback) {
if (is_array($callback)) {
// must be a class method
list($class, $method) = $callback;
return new ReflectionMethod($class, $method);
}
// class::method syntax
if (is_string($callback) && strpos($callback, "::") !== false) {
list($class, $method) = explode("::", $callback);
return new ReflectionMethod($class, $method);
}
// objects as functions (PHP 5.3+)
if (version_compare(PHP_VERSION, "5.3.0", ">=") &&
method_exists($callback, "__invoke")) {
return new ReflectionMethod($callback, "__invoke");
}
// assume it's a function
return new ReflectionFunction($callback);
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 75.172.104.223
----
Manual Page -- http://www.php.net/manual/en/class.reflectionfunction.php
Edit -- https://master.php.net/note/edit/100570
Del: integrated -- https://master.php.net/note/delete/100570/integrated
Del: useless -- https://master.php.net/note/delete/100570/useless
Del: bad code -- https://master.php.net/note/delete/100570/bad+code
Del: spam -- https://master.php.net/note/delete/100570/spam
Del: non-english -- https://master.php.net/note/delete/100570/non-english
Del: in docs -- https://master.php.net/note/delete/100570/in+docs
Del: other reasons-- https://master.php.net/note/delete/100570
Reject -- https://master.php.net/note/reject/100570
Search -- https://master.php.net/manage/user-notes.php