简体   繁体   English

如何知道方法在PHP中是Public,Protected还是Private?

[英]How to know if the method is Public, Protected or Private in PHP?

Here are three methods function_one , function_two and function_three in Example class. 下面是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;
        }
    }
}

So, I want to know which Access Modifier ( public , protected , private ) is the method. 因此,我想知道哪种访问修饰符( publicprotectedprivate )是该方法。 The imaginary is_public_method should return true because function_three is public method. 虚构的is_public_method应该返回true,因为function_threepublic方法。 Is there way to do this? 有办法吗?

You can do this using ReflectionClass and ReflectionMethod : 您可以使用ReflectionClassReflectionMethod来做到这一点:

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

您需要查看ReflectionMethodisPublic方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM