如何知道该方法在PHP中是Public、Protected还是Private


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

这里有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方法。有办法做到这一点吗?

您可以使用ReflectionClassReflectionMethod:

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

您需要查看ReflectionMethod的isPublic方法。