Longhorn PHP 2023 - Call for Papers

is_callable

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

is_callableBir değişkenin değerinin bir işlev olarak çağrılabilirliğini doğrular

Açıklama

is_callable(mixed $değer, bool $sözdizimsel = false, string &$isim = null): bool

değer callable türündeyse true döner.

Bağımsız Değişkenler

değer

Bir işlev ismi.

sözdizimsel

true atanırsa işlev bağımsız değişkeninde bir işlev veya yöntem ismi belirtildiği varsayımıyla doğrulama yapılmaya çalışılır. Sadece dizge olmayan veya bir geriçağırım işlevi olarak kullanılabilecek geçerli bir yapıya sahip olmayan bir dizi içeren basit değişkenler reddedilir. Geçerli bir dizinin 2 girdisi olacağı varsayılır; ilki bir nesne veya bir dizge, ikincisi ise bir dizge.

isim

İşlev veya yöntemin çağrılabilir ismi. Aşağıdaki örnekteki "birSınıf::birYöntem" gibi. birSınıf::birYöntem() çağrılabilir bir duruk yöntem olsa da bunun hep böyle olması gerekmez.

Dönen Değerler

Belirtilen değer bir işlev olarak çağrılabiliyorsa true, aksi takdirde false döner.

Örnekler

Örnek 1 - is_callable() örneği

<?php
// Bir değişkenin içeriğinin bir işlev olarak
// çağrılabilirliğinin sınanması.

//
// Bir işlev içeren basit bir değişken
//

function bir_işlev()
{
}

$işlevDeğişkeni = 'bir_işlev';

var_dump(is_callable($işlevDeğişkeni, false, $isim)); // bool(true)

echo $isim, "\n"; // bir_işlev

//
// Bir yöntem içeren bir dizi
//

class birSınıf {

function
birYöntem()
{
}

}

$birNesne = new birSınıf();

$yöntemDeğişkeni = array($birNesne, 'birYöntem');

var_dump(is_callable($yöntemDeğişkeni, true, $isim)); // bool(true)

echo $isim, "\n"; // birSınıf::birYöntem

?>

Örnek 2 - is_callable() ve kurucular

is_callable() kurucuları çağrılabilir saymaz.

<?php

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

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

Yukarıdaki örneğin çıktısı:

bool(false)
bool(false)

Notlar

  • Bir nesne __invoke() örnekliyorsa nesne ismi çağrılabilirdir ve geçerli etki alanında görünür olur.
  • Bir sınıf __callStatic() örnekliyorsa sınıf ismi çağrılabilirdir.
  • Bir nesne __call() örnekliyorsa, nesne ismi çağrılabilirdir ve nesne üzerindeki her yöntem için, yöntem tanımlanmamış olsa bile true döndürür.
  • Bir çağrılabilir, sınıfın ismiyle çağrılırsa otomatik yüklemeyi tetikleyebilir.

Ayrıca Bakınız

add a note

User Contributed Notes 7 notes

up
32
izharaazmi at gmail dot com
7 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
13
mohamed dot elidrissi at protonmail dot com
1 year 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
2
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
3
rahadotaboulfethatgmail.com
15 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
2
Quis strrev TA omicidio strrev TOD com
15 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
up
0
php dot net at scare dot ca
11 months ago
Note that is_callable('_') will return true, since there is a built-in function called "_". It is an alias for gettext().

https://www.php.net/manual/en/function.gettext.php

Searching this site for a function named "_" does not return a result, but it's the first result in a search for "_()".
up
-2
fgm at osinet dot fr
11 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
?>
To Top