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

[函数]ReflectionMethod::isProtected()函数—用法及示例

发布于 2025-05-03 19:36:52
0
32

函数名称:ReflectionMethod::isProtected()

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

函数说明:ReflectionMethod::isProtected() 方法用于检查当前方法是否为受保护的。

用法示例:

<?php
class MyClass {
    protected function myProtectedMethod() {
        // some code here
    }
}

$reflectionClass = new ReflectionClass('MyClass');
$reflectionMethod = $reflectionClass->getMethod('myProtectedMethod');

if ($reflectionMethod->isProtected()) {
    echo "This method is protected.";
} else {
    echo "This method is not protected.";
}
?>

在上面的示例中,我们首先定义了一个名为MyClass的类,并在其中声明了一个受保护的方法myProtectedMethod()。然后,我们使用ReflectionClass类创建了一个反射类对象,并使用getMethod()方法获取了myProtectedMethod()方法的反射对象。

接下来,我们使用isProtected()方法检查该方法是否为受保护的。如果是受保护的方法,则输出"This method is protected.";否则,输出"This method is not protected."。

请注意,为了使用ReflectionMethod类,您需要启用Reflection扩展。

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

9545

帖子

31

小组

3242

积分

赞助商广告
站长交流