【问题标题】:How to know if the method is Public, Protected or Private in PHP?如何知道该方法在 PHP 中是公共的、受保护的还是私有的?
【发布时间】:2013-06-24 20:46:27
【问题描述】:

这是Example类中的三个方法function_onefunction_twofunction_three

class Example
{
    private function function_one() { ... }

    protected function function_two() { ... }

    public function function_three() { ... }

    public function check_here()
    {
        if (is_public_method('function_three')) {
            return true;
        } else {
            return false;
        }
    }
}

所以,我想知道哪个访问修饰符(publicprotectedprivate)是方法。虚构的is_public_method 应该返回true,因为function_threepublic 方法。有没有办法做到这一点?

【问题讨论】:

    标签: php class oop methods access-modifiers


    【解决方案1】:

    您可以使用ReflectionClassReflectionMethod 来做到这一点:

    public function check_here()
    {
        $obj = new ReflectionClass($this);
        return $obj->getMethod('function_three')->isPublic();
    }
    

    【讨论】:

      【解决方案2】:

      您需要查看ReflectionMethodisPublic 方法。

      【讨论】:

        猜你喜欢
        • 2011-05-29
        • 2013-08-14
        • 2010-10-21
        • 2012-09-09
        • 2011-05-08
        • 1970-01-01
        • 2011-07-21
        • 2017-01-20
        • 1970-01-01
        相关资源
        最近更新 更多