PHP: Hypertext Preprocessor (original) (raw)
is_callable
(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)
is_callable — Verify that a value can be called as a function from the current scope
Description
Parameters
value
The value to be checked.
syntax_only
If set to [true](reserved.constants.php#constant.true)
the function only verifies thatvalue
might be a function or method. It will reject any values that are not invokable objects,Closure, strings, or arrays that do not have a valid structure to be used as a callback. A valid callable array has 2 entries, the first of which is an object or a string, and the second a string.
callable_name
Receives the "callable name", e.g."SomeClass::someMethod"
. Note, however, that despite the implication that SomeClass::someMethod()
is a callable static method, this is not the case.
Return Values
Returns [true](reserved.constants.php#constant.true)
if value
is callable, [false](reserved.constants.php#constant.false)
otherwise.
Examples
Example #1 Checking whether a string can be called as a function
<?phpfunction someFunction() {}$functionVariable = 'someFunction';var_dump(is_callable($functionVariable, false, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>a</mi><mi>l</mi><mi>l</mi><mi>a</mi><mi>b</mi><mi>l</mi><msub><mi>e</mi><mi>n</mi></msub><mi>a</mi><mi>m</mi><mi>e</mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo separator="true">;</mo><mi>v</mi><mi>a</mi><msub><mi>r</mi><mi>d</mi></msub><mi>u</mi><mi>m</mi><mi>p</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">callable_name));var_dump(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">c</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">ll</span><span class="mord mathnormal">ab</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord"><span class="mord mathnormal">e</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">n</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">am</span><span class="mord mathnormal">e</span><span class="mclose">))</span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0278em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">d</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">u</span><span class="mord mathnormal">m</span><span class="mord mathnormal">p</span><span class="mopen">(</span></span></span></span>callable_name);?>
The above example will output:
bool(true) string(12) "someFunction"
Example #2 Checking whether an array can be called as a function
<?phpclass SomeClass { public function someMethod() {} }$anObject = new SomeClass();$methodVariable = [$anObject, 'someMethod'];var_dump(is_callable($methodVariable, true, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>a</mi><mi>l</mi><mi>l</mi><mi>a</mi><mi>b</mi><mi>l</mi><msub><mi>e</mi><mi>n</mi></msub><mi>a</mi><mi>m</mi><mi>e</mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo separator="true">;</mo><mi>v</mi><mi>a</mi><msub><mi>r</mi><mi>d</mi></msub><mi>u</mi><mi>m</mi><mi>p</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">callable_name));var_dump(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">c</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">ll</span><span class="mord mathnormal">ab</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord"><span class="mord mathnormal">e</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">n</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">am</span><span class="mord mathnormal">e</span><span class="mclose">))</span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0278em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">d</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">u</span><span class="mord mathnormal">m</span><span class="mord mathnormal">p</span><span class="mopen">(</span></span></span></span>callable_name);?>
The above example will output:
bool(true) string(21) "SomeClass::someMethod"
Example #3 is_callable() and constructors
Despite the fact that constructors are the methods that are called when an object is created, they are not static methods andis_callable() will return [false](reserved.constants.php#constant.false)
for them. It's not possible to use is_callable() to check if a class can be instantiated from the current scope.
`<?phpclass Foo
{
public function __construct() {}
public function
foo() {}
}var_dump(
is_callable(['Foo', '__construct']),
is_callable(['Foo', 'foo'])
);$foo = new Foo();
var_dump(is_callable([$foo, '__construct']));?>`
The above example will output:
bool(false) bool(false) bool(true)
Notes
- An object is always callable if it implements __invoke(), and that method is visible in the current scope.
- A class name is callable if it implements __callStatic().
- If an object implements __call(), then this function will return
[true](reserved.constants.php#constant.true)
for any method on that object, even if the method is not defined. - This function may trigger autoloading if called with the name of a class.
See Also
- call_user_func() - Call the callback given by the first parameter
- function_exists() - Return true if the given function has been defined
- method_exists() - Checks if the class method exists
Found A Problem?
9 years ago
`If the target class has __call() magic function implemented, then is_callable will ALWAYS return TRUE for whatever method you call it.
is_callable does not evaluate your internal logic inside __call() implementation (and this is for good).
Therefore every method name is callable for such classes.
Hence it is WRONG to say (as someone said):
...is_callable will correctly determine the existence of methods made with __call...
Example:
<?php
class TestCallable
{
public function testing()
{
return "I am called.";
}
public function
__call($name, $args)
{
if($name == 'testingOther')
{
return call_user_func_array(array($this, 'testing'), $args);
}
}
}$t = new TestCallable();
echo $t->testing(); // Output: I am called.
echo $t->testingOther(); // Output: I am called.
echo t−>working();//Output:(null)echoiscallable(array(t->working(); // Output: (null)echo is_callable(array(t−>working();//Output:(null)echoiscallable(array(t, 'testing')); // Output: TRUE
echo is_callable(array($t, 'testingOther')); // Output: TRUE
echo is_callable(array($t, 'working')); // Output: TRUE, expected: FALSE
?>
`
mohamed dot elidrissi at protonmail dot com ¶
3 years ago
`Note that -- as mentioned in the migration guide-- starting from PHP 8.0, is_callable() will not work with non-static methods if you use a class name, instead an object of the class should be provided:
`