is_callable

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

is_callable Verifică dacă conținutul unei variabile poate fi apelat în calitate de funcție

Descrierea

is_callable ( mixed $var [, bool $syntax_only = false [, string &$callable_name ]] ) : bool

Verifică dacă valoarea este de tip callable.

Parametri

var

Valoarea spre a fi verificată.

syntax_only

Dacă este stabilit în true atunci funcția verifică numai dacă var poate fi o funcție sau o metodă. Ea doar va respinge variabilele simple care nu sunt stringuri, sau tablourile care nu au structuri valide pentru a fi utilizate în calitate de funcție de apel invers. Cele valide trebuie să aibă doar 2 înregistrări, prima dintre care este un obiect sau un string, și a doua - un string.

callable_name

Primește "denumirea apelabilă". În exemplul de mai jos aceasta este "someClass::someMethod". Observați însă că în ciuda faptului că someClass::SomeMethod() este o metodă apelabilă static, acesta nu este cazul.

Valorile întoarse

Întoarce true dacă var este apelabilă, false în caz contrar.

Exemple

Example #1 Exemplu is_callable()

<?php
//  Cum de verificat o variabilă dacă ea poate fi apelată în calitate de
//  funcție

//
//  O variabilă simplă ce conține o funcție
//

function someFunction()
{
}

$functionVariable 'someFunction';

var_dump(is_callable($functionVariablefalse$callable_name));  // bool(true)

echo $callable_name"\n";  // someFunction

//
//  Tablou conținând o metodă
//

class someClass {

  function 
someMethod()
  {
  }

}

$anObject = new someClass();

$methodVariable = array($anObject'someMethod');

var_dump(is_callable($methodVariabletrue$callable_name));  //  bool(true)

echo $callable_name"\n";  //  someClass::someMethod

?>

Example #2 is_callable() și constructori

Începând cu PHP 5.3.0 is_callable() raportează constructorii ca nefiind apelabili. Aceasta afectează constructorii de stil PHP 5 (__construct), precum și constructorii de stil PHP 4 (de ex. metode cu aceeași denumire ca și clasa). Anterior ambele cazuri erau considerate apelabile.

<?php

class Foo
{
    public function 
__construct() {}
    public function 
foo() {}
}

var_dump(
    
is_callable(array('Foo''__construct')),
    
is_callable(array('Foo''foo'))
);

Exemplul de mai sus va afișa:

bool(false)
bool(false)

A se vedea și

add a note add a note

User Contributed Notes 6 notes

up
31
izharaazmi at gmail dot com
8 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)

echo is_callable(array($t, 'testing'));       // Output: TRUE
echo is_callable(array($t, 'testingOther'));  // Output: TRUE
echo is_callable(array($t, 'working'));       // Output: TRUE, expected: FALSE
?>
up
2
rahadotaboulfethatgmail.com
16 years ago
is_callable generates an [E_STRICT] error if the  tested method cannot be called staticly. (and returns the good value)

I used @is_called
i'm using php 5.2.1
up
1
mohamed dot elidrissi at protonmail dot com
2 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:

<?php

class Test
{
    public function
method1() { }
    public static function
method2() { }
}

// Pre PHP 8
var_dump(is_callable(array('Test', 'method1'))); // bool(true)
var_dump(is_callable(array('Test', 'method2'))); // bool(true)

// Post PHP 8
var_dump(is_callable(array('Test', 'method1'))); // bool(false)
var_dump(is_callable(array('Test', 'method2'))); // bool(true)
var_dump(is_callable(array(new Test, 'method1'))); // bool(true)

?>
up
0
jlh
4 years ago
The story about __call() is a bit more complicated unfortunately. It will always return true ONLY if you pass an instance of a class, not if you pass the class name itself:

<?php
class MyClass {
    public function
method() { }
    public function
__call($name, $arguments) { }
}

is_callable([MyClass::class, 'method']); // true
is_callable([new MyClass(), 'method']); // true
is_callable([MyClass::class, 'other']); // false!!!
is_callable([new MyClass(), 'other'])); // true
up
0
fgm at osinet dot fr
12 years ago
Note that, for the purpose of this function, an abstract method, although necessarily non-callable since it does not have a body, is still considered to be callable:

<?php
abstract class Foo {
  abstract function
bar();
}

echo
is_callable(array('Foo', 'bar'));
// display: 1
?>
up
0
Quis strrev TA omicidio strrev TOD com
16 years ago
is_callable() does _not_ check wheter this function is disabled by php.ini's disable_functions

use:

<?PHP
function is_disabled($function) {
 
$disabled_functions=explode(',',ini_get('disable_functions'));
  return
in_array($function, $disabled_functions);
}
?>

I`m running PHP 5.2.4
To Top