Try it like this.
<?php class test { private $a = 0; protected $b = 0; public $c = 0; public function echoprop() { $reflector = new ReflectionClass(get_class($this)); foreach($this as $key => $val) { $prop = $reflector->getProperty($key); if( $prop->isPrivate() ) { echo 'Private'; } else if( $prop->isProtected() ) { echo 'Protected'; } else if( $prop->isPublic() ) { echo 'Public'; } echo "$".$key." = ".$val.";\n"; } } } $var = new test; $var->echoprop();
I can also offer such a perverted version :) without ReflectionClass. The PHP function get_class_vars returns an array of class methods that are within the scope of this function.
That is, so you can get all the methods of visibility.
class test { private $a = 0; protected $b = 0; public $c = 0; public function getVisibleProps() { return get_class_vars(get_class($this)); } } class testExtend extends test { public function getVisibleProps() { return get_class_vars(get_class($this)); } } $var = new test; $allPublicProtectedPrivateProps = $var->getVisibleProps(); $varExtend = new testExtend(); $allPublicProtectedPropers = $varExtend ->getVisibleProps(); $allPublicProps = get_class_vars(get_class($var));
Next, you just need to get the allPublicProtectedPrivateProps , allPublicProtectedPropers and $allPublicProps and get the corresponding properties with their scopes.
And here is another solution. But needs parsing.
class test { public $var1 = 1; protected $var2 = 2; private $var3 = 3; static $var4 = 4; public function toArray() { return (array) $this; } } $t = new test; print_r($t->toArray()); /* outputs: Array ( [var1] => 1 [ * var2] => 2 [ test var3] => 3 ) */
By key, you can understand the scope.
But this is too perversely obtained - well, not programmerly, so to speak :)