note 116877 added to function.is-callable
| From: | ffalekatbanfidotpl | Date: | Fri, 13 Mar 2015 17:42:02 +0000 |
| Subject: | note 116877 added to function.is-callable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201939@lists.php.net to get a copy of this message | ||
Example how is_callable works:
<?php
$counter = 0;
class TestClassOne {
function testMethod(){
global $counter;
return ++$counter;
}
static function testMethodStatic(){
global $counter;
return ++$counter;
}
}
class TestClassTwo {
function __invoke() {
global $counter;
return ++$counter;
}
}
function test_function() {
global $counter;
return ++$counter;
}
$p1 = 'test_function';
$p2 = new TestClassOne();
$p3 = ['TestClassOne', 'testMethodStatic'];
$p4 = [new TestClassOne(), 'testMethod'];
$p5 = new TestClassTwo();
$p6 = function() {
global $counter;
return ++$counter;
};
foreach ([$p1, $p2, $p3, $p4, $p5, $p6] as $p) {
if(is_callable($p)){
echo 'is invokable: yes '.$p().PHP_EOL;
} else {
echo 'is invokable: no'.PHP_EOL;
}
}
?>
And output is:
is invokable: yes 1
is invokable: no
is invokable: yes 2
is invokable: yes 3
is invokable: yes 4
is invokable: yes 5
----
Server IP: 72.52.91.14
Probable Submitter: 77.79.231.146
----
Manual Page -- http://php.net/manual/en/function.is-callable.php
Edit -- https://master.php.net/note/edit/116877
Del: integrated -- https://master.php.net/note/delete/116877/integrated
Del: useless -- https://master.php.net/note/delete/116877/useless
Del: bad code -- https://master.php.net/note/delete/116877/bad+code
Del: spam -- https://master.php.net/note/delete/116877/spam
Del: non-english -- https://master.php.net/note/delete/116877/non-english
Del: in docs -- https://master.php.net/note/delete/116877/in+docs
Del: other reasons-- https://master.php.net/note/delete/116877
Reject -- https://master.php.net/note/reject/116877
Search -- https://master.php.net/manage/user-notes.php