获取PHP中对象变量的保护类型


Get Protection type of Object Variable in PHP

我正在构建一个类。我打算让这个类成为一种主父类,用于以后的许多API和数据库交互

假设它看起来像这个

class api_controller{
    public $method = 'get';
    private $table;
    protected $table_id_column;
    //Rest of code is really not needed
}

我想知道,在PHP中,如果给定名称,是否可以确定变量是publicprivate还是protected?如果是这样的话,我计划把它用作一个检查站,以确保没有子方法更改它们被限制通过继承方法访问的数据

我在谷歌上搜索了我的问题,提出了很多get_object_vars()get_class_vars()的讨论,以及关于私人、受保护和公共之间区别的精彩许多讨论。从我在PHP数据库中搜索Object/Class函数的过程中,我没有看到任何立即跳出来作为我的答案的东西。

我想这可能是一个try/catch语句,通过访问变量并查看它是否抛出错误(这会让我知道它是公共/私有的)来完成,但我不确定如何确定超过这一点。即便如此,该方法也必须是父类的成员,因此它可以访问自己的所有私有变量。

有什么想法吗?

使用反射

$class = new ReflectionClass('api_controller');
$property = $class->getProperty('method');
// then you could check by
// there are also methods of isProtected, isPublic, etc...
if ($property->isPrivate()) {
  // ..
}