note 100238 deleted from function.method-exists by danbrown
| From: | danbrown@php.net | Date: | Sun, 03 Oct 2010 22:06:13 +0000 |
| Subject: | note 100238 deleted from function.method-exists by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172915@lists.php.net to get a copy of this message | ||
Note Submitter: colin
----
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