class MyClass { public $publicProperty; protected $protectedProperty; private $privateProperty; } $reflectionClass = new ReflectionClass('MyClass'); $publicProperty = $reflectionClass->getProperty('publicProperty'); echo $publicProperty->isPublic(); // outputs true
class MyClass { protected $protectedProperty; } $reflectionClass = new ReflectionClass('MyClass'); $protectedProperty = $reflectionClass->getProperty('protectedProperty'); echo $protectedProperty->isPublic(); // outputs falseIn this example, we have defined a class MyClass with only one property - protected. We then create a new ReflectionClass object for this class and use the getProperty method to get the ReflectionProperty object for the protected property. We then use the isPublic method to check if the property is public or not, which returns false in this case. Package/Library: The ReflectionProperty class is a part of the PHP core library and is part of the Reflection extension.