大网数据广告活动
PHP 语言与运行手册

ReflectionClass::hasMethod · 检查方法是否已定义

· 52CE 我爱测

ReflectionClass::hasConstant() - 检查常量是否已经定义 ReflectionClass::hasProperty() - 检查属性是否已定义

ReflectionClass::hasMethod

(PHP 5 >= 5.1.2, PHP 7, PHP 8)

ReflectionClass::hasMethod — 检查方法是否已定义

说明

public function ReflectionClass::hasMethod(string $name): bool

检查一个类中指定的方法是否已定义。

参数

name

要检查的方法的名称。

返回值

如果有这个方法返回 true,否则返回 false。

示例

示例 #1 ReflectionClass::hasMethod() 示例

<?php
Class C {
    public function publicFoo() {
        return true;
    }

    protected function protectedFoo() {
        return true;
    }

    private function privateFoo() {
        return true;
    }

    static function staticFoo() {
        return true;
    }
}

$rc = new ReflectionClass("C");

var_dump($rc->hasMethod('publicFoo'));

var_dump($rc->hasMethod('protectedFoo'));

var_dump($rc->hasMethod('privateFoo'));

var_dump($rc->hasMethod('staticFoo'));

// C should not have method bar
var_dump($rc->hasMethod('bar'));

// Method names are case insensitive
var_dump($rc->hasMethod('PUBLICfOO'));
?>

以上示例会输出:

bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。