How can I detect if a class property is private or protected without using external libraries (pure PHP only)? How can I check if I can set the property from outside the class or I cannot?
Use Reflection.
<?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());
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With