method_exists

クラスメソッドが存在するかどうかを確認する

説明

bool method_exists(objectstring $object_or_class, string $method)

指定した object_or_class にクラスメソッドが存在するかどうかを調べます。

パラメータ

object_or_class

オブジェクトのインスタンス、あるいはクラス名。

method

メソッドの名前。

戻り値

method で指定したメソッドが 指定した object_or_class において定義されている場合に true、そうでない場合に false を返します。

例1 method_exists の例

<?php
$directory 
= new Directory('.');
var_dump(method_exists($directory,'read'));
?>

上の例の出力は以下となります。

bool(true)

例2 staticメソッドに対する method_exists の例

<?php
var_dump
(method_exists('Directory','read'));
?>

上の例の出力は以下となります。

bool(true)

注意

注意:

この関数を使うと、未知のクラスに対しては登録済みの autoloader を使用します。

参考

  • function_exists
  • is_callable
  • class_exists