phpでfunction_exists 、 method_existsとis_callable

1730 ワード

function_exists — Return  TRUE  if the given function has been defined 
method_exists  — Checks if the class method exists
is_callable — Verify that the contents of a variable can be called as a function
function_existsの簡単な点は、関数が定義されているかどうかを判断することです.method_existsはクラス内のメソッドが存在しないと判断するis_callable検出パラメータが正当な呼び出し可能構造であるか否か
戻り値はbool
でもパラメータが違う
function_existsはパラメータ関数名$stringが1つしかありません
method_exists 2つのパラメータ$objectクラスオブジェクト$stringメソッド名
is_callableの3つのパラメータ$var mixedはstring array$syntax_であってもよいonly  bool   $callback_name  string
もしis_callableの最初のパラメータはstringでfunction_exists類似配列であればmethod_exists  
でも違う
method_existsはクラスメソッドの定義範囲public protected privateを考慮せずis_callableはprotected privateによってfalseを返す方法です
is_callableは呼び出すと判断します_マジックの方法で判断し、method_existsはphpを使用しません.Netの例は次のように説明されています.
Example:class Test {     public function testing($not = false) {         $not = $not ? 'true' : 'false';         echo "testing - not: $not";     }          public function __call($name, $args) {         if(preg_match('/^not([A-Z]\w+)$/', $name, $matches)) {             $fn_name = strtolower($matches[1]);             if(method_exists($this, $fn_name)) {                 $args[] = true; //add NOT boolean to args                 return call_user_func_array(array($this, $matches[1]), $args);             }         }         die("No method with name: $name");     } }$t = new Test();$t->testing();$t->notTesting(); echo "exists: ".method_exists($t, 'notTesting').''; echo "callable: ".is_callable(array($t, 'notTesting'));?>Output:testing - not: falsetesting - not: trueexists:callable: 1