method_exists

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

method_exists檢查類的方法是否存在

說明

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

檢查類的方法是否存在于指定的 object中。

參數(shù)

object

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

method_name

方法名。

返回值

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

注釋

注意:

如果此類不是已知類,使用此函數(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)

參見