array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'uk', ), 'this' => array ( 0 => 'function.is-callable.php', 1 => 'is_callable', 2 => 'Verify that a value can be called as a function from the current scope', ), 'up' => array ( 0 => 'ref.var.php', 1 => 'Variable handling Функції', ), 'prev' => array ( 0 => 'function.is-bool.php', 1 => 'is_bool', ), 'next' => array ( 0 => 'function.is-countable.php', 1 => 'is_countable', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/var/functions/is-callable.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>

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

Опис

is_callable(mixed $value, bool $syntax_only = false, string &$callable_name = null): bool

Verifies that value is a callable, or that it can be called using the call_user_func() function.

Параметри

value

The value to be checked.

syntax_only

If set to true the function only verifies that value 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.

Значення, що повертаються

Returns true if value is callable, false otherwise.

Приклади

Приклад #1 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"

Приклад #2 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"

Приклад #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 and is_callable() will return false for them. It's not possible to use is_callable() to check if a class can be instantiated from the current scope.

<?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)

Примітки

Прогляньте також