Linux 拨号vps windows公众号手机端

PHP的method_exists函数怎么使用

lewis 6年前 (2019-02-09) 阅读数 7 #程序编程

method_exists函数用于检查一个对象或类是否具有指定的方法。它的使用方式如下:

bool method_exists ( mixed $object , string $method_name )

参数说明:

  • $object:要检查的对象或类名。

  • $method_name:要检查的方法名。

返回值:

  • 如果对象或类中存在指定的方法,则返回 true;否则返回 false。

示例代码:

class MyClass {
public function myMethod() {
echo "Hello, World!";
}
}
$object = new MyClass();
// 检查对象是否具有指定的方法
if (method_exists($object, 'myMethod')) {
echo "对象具有 myMethod 方法";
} else {
echo "对象没有 myMethod 方法";
}
// 检查类是否具有指定的方法
if (method_exists('MyClass', 'myMethod')) {
echo "类具有 myMethod 方法";
} else {
echo "类没有 myMethod 方法";
}

输出结果:

对象具有 myMethod 方法
类具有 myMethod 方法
版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门