note 100238 added to function.method-exists
| From: | colin at osu1 dot php dot net | Date: | Sun, 03 Oct 2010 18:31:38 +0000 |
| Subject: | note 100238 added to function.method-exists | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172911@lists.php.net to get a copy of this message | ||
I haven't seen anyone note this before, but is_callable will correctly determine the existence
of methods made with __call. The method_exists function will not.
Example:
<?php
class Test {
public function testing($not = false) {
$not = $not ? 'true' : 'false';
echo "testing - not: $not<br/>";
}
public function __call($name, $args) {
if(preg_match('/^not([A-Z]\w+)$/', $name, $matches)) {
$fn_name = strtolower($matches[1]);
if(method_exists($this, $fn_name)) {
$args[] = true; // add NOT boolean to args
return call_user_func_array(array($this, $matches[1]), $args);
}
}
die("No method with name: $name<br/>");
}
}
$t = new Test();
$t->testing();
$t->notTesting();
echo "exists: ".method_exists($t, 'notTesting').'<br/>';
echo "callable: ".is_callable(array($t, 'notTesting'));
?>
Output:
testing - not: false
testing - not: true
exists:
callable: 1
----
Server IP: 92.48.119.85
Probable Submitter: 216.165.158.51
----
Manual Page -- http://www.php.net/manual/en/function.method-exists.php
Edit -- https://master.php.net/note/edit/100238
Del: integrated -- https://master.php.net/note/delete/100238/integrated
Del: useless -- https://master.php.net/note/delete/100238/useless
Del: bad code -- https://master.php.net/note/delete/100238/bad+code
Del: spam -- https://master.php.net/note/delete/100238/spam
Del: non-english -- https://master.php.net/note/delete/100238/non-english
Del: in docs -- https://master.php.net/note/delete/100238/in+docs
Del: other reasons-- https://master.php.net/note/delete/100238
Reject -- https://master.php.net/note/reject/100238
Search -- https://master.php.net/manage/user-notes.php