[英]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_one
, function_two
和function_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. 因此,我想知道哪种访问修饰符(
public
, protected
, private
)是该方法。 The imaginary is_public_method
should return true because function_three
is public
method. 虚构的
is_public_method
应该返回true,因为function_three
是public
方法。 Is there way to do this? 有办法吗?
You can do this using ReflectionClass
and ReflectionMethod
: 您可以使用
ReflectionClass
和ReflectionMethod
来做到这一点:
public function check_here()
{
$obj = new ReflectionClass($this);
return $obj->getMethod('function_three')->isPublic();
}
您需要查看ReflectionMethod的isPublic方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.