首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

[函数]ReflectionClass::hasMethod()函数—用法及示例

发布于 2025-05-03 14:04:39
0
41

函数名:ReflectionClass::hasMethod()

适用版本:PHP 5 >= 5.1.0, PHP 7

函数说明:ReflectionClass::hasMethod() 方法用于检查类是否具有指定的方法。

语法:public bool ReflectionClass::hasMethod ( string $name )

参数:

  • $name:要检查的方法的名称。

返回值:

  • 如果类具有指定的方法,则返回 true,否则返回 false。

示例:

class MyClass {
    public function myMethod() {
        // ...
    }
}

$reflection = new ReflectionClass('MyClass');

// 检查类是否具有名为 "myMethod" 的方法
if ($reflection->hasMethod('myMethod')) {
    echo 'MyClass 类具有 myMethod 方法';
} else {
    echo 'MyClass 类不具有 myMethod 方法';
}

输出结果:

MyClass 类具有 myMethod 方法

在上面的示例中,我们首先创建了一个名为 MyClass 的类,该类具有一个名为 myMethod 的公共方法。然后,我们使用 ReflectionClass 类创建了一个类的反射实例 $reflection。最后,我们使用 $reflection->hasMethod('myMethod') 检查类是否具有名为 myMethod 的方法,并根据结果输出相应的信息。由于 MyClass 类确实具有 myMethod 方法,因此输出结果为 MyClass 类具有 myMethod 方法

评论
一个月内的热帖推荐
啊龙
Lv.1普通用户

9545

帖子

31

小组

3242

积分

赞助商广告
站长交流