如何检测类属性是私有还是受保护

Sho*_*hoe 4 php oop

如何在使用外部库的情况下检测类属性是私有还是受保护(仅限纯PHP)?如何检查我是否可以从课外设置属性,或者我不能?

Ale*_*pin 8

使用反射.

<?php
    class Test {
        private $foo;
        public $bar;
    }

    $reflector = new ReflectionClass(get_class(new Test()));

    $prop = $reflector->getProperty('foo');
    var_dump($prop->isPrivate());

    $prop = $reflector->getProperty('bar');
    var_dump($prop->isPrivate());
?>
Run Code Online (Sandbox Code Playgroud)