method_exists

(PHP 4, PHP 5, PHP 7, PHP 8)

method_exists檢查類(lèi)的方法是否存在

說(shuō)明

method_exists(mixed $object, string $method_name): bool

檢查類(lèi)的方法是否存在于指定的 object中。

參數(shù)

object

對(duì)象示例或者類(lèi)名。

method_name

方法名。

返回值

如果 method_name 所指的方法在 object 所指的對(duì)象類(lèi)中已定義,則返回 true,否則返回 false。

注釋

注意:

如果此類(lèi)不是已知類(lèi),使用此函數(shù)會(huì)使用任何已注冊(cè)的 autoloader

范例

示例 #1 method_exists() 例子

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

以上例程會(huì)輸出:

bool(true)

示例 #2 Static method_exists() 例子

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

以上例程會(huì)輸出:

bool(true)

參見(jiàn)

  • function_exists() - 如果給定的函數(shù)已經(jīng)被定義就返回 true
  • is_callable() - 檢測(cè)參數(shù)是否為合法的可調(diào)用結(jié)構(gòu)
  • class_exists() - 檢查類(lèi)是否已定義