Dave
Dave

Reputation: 3448

PHP: Identifying the visibility of a class variable

Suppose I have the class:

class MyClass {

    protected $protected;
    private $_private;

    public function __get($name) {
        return $this->{$name};
    }

}

I want to "magically" get protected variables but not private variables. Is there a built-in PHP function that will help me identify the visibility of a class variable?

Upvotes: 1

Views: 83

Answers (1)

KingCrunch
KingCrunch

Reputation: 131891

$refClass = new ReflectionClass('MyClass');
foreach ($refClass->getProperties() as $property) {
  if ($property->isProtected()) echo $property->getName();
}

Upvotes: 2

Related Questions