is_callable
is_callable
Verify that a value can be called as a function from the current scope
bool **is_callable** mixed $value bool $syntax_only string $callable_name
Verifies that is a ,
or that it can be called using the
function.
value``callable``call_user_func
valueThe value to be checked.
syntax_only
If set to true the function only verifies that
might be a function or method. It will
reject any values that are not objects,
, 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.
valueinvokableClosure
callable_name
Receives the "callable name", e.g.
. Note, however, that despite
the implication that is a
callable static method, this is not the case.
"SomeClass::someMethod"``SomeClass::someMethod()
Returns true if is callable, false
otherwise.
value
Voorbeeld: Checking whether a string can be called as a function
<?php
function someFunction() {}
$functionVariable = 'someFunction';
var_dump(is_callable($functionVariable, false, $callable_name));
var_dump($callable_name);
?>
bool(true)
string(12) "someFunction"
Voorbeeld: Checking whether an array can be called as a function
<?php
class SomeClass
{
public function someMethod() {}
}
$anObject = new SomeClass();
$methodVariable = [$anObject, 'someMethod'];
var_dump(is_callable($methodVariable, true, $callable_name));
var_dump($callable_name);
?>
bool(true)
string(21) "SomeClass::someMethod"
Voorbeeld: and constructors
Despite the fact that constructors are the methods that are called when
an object is created, they are not static methods and
will return false for them. It's not
possible to use to check if a class can
be instantiated from the current scope.
`is_callable``is_callable`
<?php
class 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']));
?>
bool(false)
bool(false)
bool(true)
__invoke()__callStatic()__call()
call_user_func``function_exists``method_exists