Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

iterate public memebers of a class

Status
Not open for further replies.

Geates

Programmer
Aug 25, 2009
1,566
US
Short of renaming the public members, is there a way to iterate only public members of a class.

Something like

Code:
class aClass
{
   private A
   private B
   public C
   public D

   function iteratePublic()
   {
      foreach($this as $key = $val)
         if (isPublic($key))
            print $key." is public";
   }
}

output:
C is public
D is public

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
here is one way of doing it (you will also get protected vars)

Code:
<?php
class a{
        public function getPublicProperties(){
                foreach($this as $key=>$val):
                        echo $key . "\t" . $val . "\n";
                endforeach;
        }
}

class b extends a{
        public $a = 'foo';
        public $b = 'bar';
        protected $c = 'bing';
        private $d = 'bong';

}

$a = new b;
$a->getPublicProperties();
?>


or you could use a reflection object.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top