note 99617 added to book.reflection

From: Date: Thu, 26 Aug 2010 16:19:16 +0000
Subject: note 99617 added to book.reflection
Groups: php.notes 
Request: Send a blank email to php-notes+get-172098@lists.php.net to get a copy of this message
Here is a code snippet for some of us who are just beginning with reflection. I have a simple class below with two properties and two methods. We will use reflection classes to populate the properties dynamically and then print them: <?php class A { public $one = ''; public $two = ''; //Constructor public function __construct() { //Constructor } //print variable one public function echoOne() { echo $this->one."\n"; } //print variable two public function echoTwo() { echo $this->two."\n"; } } //Instantiate the object $a = new A(); //Instantiate the reflection object $reflector = new ReflectionClass('A'); //Now get all the properties from class A in to $properties array $properties = $reflector->getProperties(); $i =1; //Now go through the $properties array and populate each property foreach($properties as $property) { //Populating properties $a->{$property->getName()}=$i; //Invoking the method to print what was populated $a->{"echo".ucfirst($property->getName())}()."\n"; $i++; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 208.65.144.247 ---- Manual Page -- http://www.php.net/manual/en/book.reflection.php Edit -- https://master.php.net/note/edit/99617 Del: integrated -- https://master.php.net/note/delete/99617/integrated Del: useless -- https://master.php.net/note/delete/99617/useless Del: bad code -- https://master.php.net/note/delete/99617/bad+code Del: spam -- https://master.php.net/note/delete/99617/spam Del: non-english -- https://master.php.net/note/delete/99617/non-english Del: in docs -- https://master.php.net/note/delete/99617/in+docs Del: other reasons-- https://master.php.net/note/delete/99617 Reject -- https://master.php.net/note/reject/99617 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172098) next »