note 56096 added to function.get-object-vars

From: Date: Tue, 23 Aug 2005 13:08:18 +0000
Subject: note 56096 added to function.get-object-vars
Groups: php.notes 
Request: Send a blank email to php-notes+get-94077@lists.php.net to get a copy of this message
If you want to access all properties (private, protected, public) of a class and his base class(es) from outside the object you can take a look in the code below. You can even reffrence them. Example: <?php // Dummy class to act as parent class. class Dummy { private $d1 = 1; protected $d2 = 2; public $d3 = 3; } // The class to test on. class Test extends Dummy { private $t1 = 11; protected $t2 = 12; public $t3 = 13; } // Instance of the test class. $test =& new Test(); // The propertynames of the test class and his parent. $nameD1 = "\0Dummy\0d1"; // class Dummy, private $d1 $nameD2 = "\0*\0d2"; // class Dummy, protected $d2 $nameD3 = "d3"; // class Dummy, public $d3 $nameT1 = "\0Test\0t1"; // class Test, private $t1 $nameT2 = "\0*\0t2"; // class Test, protected $t2 $nameT3 = "t3"; // class Test, public $t3 // Printing all members (private, protected, public) of test class and parent. // These are the values at construction. print("Original object:\n"); print("\t\$d1 = ".$test->$nameD1."\n"); print("\t\$d2 = ".$test->$nameD2."\n"); print("\t\$d3 = ".$test->$nameD3."\n"); print("\t\$t1 = ".$test->$nameT1."\n"); print("\t\$t2 = ".$test->$nameT2."\n"); print("\t\$t3 = ".$test->$nameT3."\n"); print("\n"); // Create new values to use as reffrence. $varD1 = 31; $varD2 = 32; $varD3 = 33; $varT1 = 41; $varT2 = 42; $varT3 = 43; // Reffrence these to the class properties. $test->$nameD1 =& $varD1; $test->$nameD2 =& $varD2; $test->$nameD3 =& $varD3; $test->$nameT1 =& $varT1; $test->$nameT2 =& $varT2; $test->$nameT3 =& $varT3; // Printing all members (private, protected, public) of test class and parent. // The values have changed by assigning reffrence to them. print("Object Changed by reffrence (1):\n"); print("\t\$d1 = ".$test->$nameD1."\n"); print("\t\$d2 = ".$test->$nameD2."\n"); print("\t\$d3 = ".$test->$nameD3."\n"); print("\t\$t1 = ".$test->$nameT1."\n"); print("\t\$t2 = ".$test->$nameT2."\n"); print("\t\$t3 = ".$test->$nameT3."\n"); print("\n"); // Change the original values. // This will change the class properties. $varD1 = 61; $varD1 = 62; $varD1 = 63; $varT1 = 71; $varT1 = 72; $varT1 = 73; // Printing all members (private, protected, public) of test class and parent. // The values have changed because the variables $varXX have been changed. print("Object Changed by reffrence (2):\n"); print("\t\$d1 = ".$test->$nameD1."\n"); print("\t\$d2 = ".$test->$nameD2."\n"); print("\t\$d3 = ".$test->$nameD3."\n"); print("\t\$t1 = ".$test->$nameT1."\n"); print("\t\$t2 = ".$test->$nameT2."\n"); print("\t\$t3 = ".$test->$nameT3."\n"); print("\n"); // Printing the object using print_r() shows the changes // have been done on the object. print("Object Changed (print_r):\n"); print_r($test); print("\n"); // If you dont now the class propertynames you can get these by casting the // object to an array. The array keys are the names $prop = array_keys((array) $test); print("Getting all class propertynames (print_r)\n"); print_r($prop); //Result /* Original object: $d1 = 1 $d2 = 2 $d3 = 3 $t1 = 11 $t2 = 12 $t3 = 13 Object Changed by reffrence (1): $d1 = 31 $d2 = 32 $d3 = 33 $t1 = 41 $t2 = 42 $t3 = 43 Object Changed by reffrence (2): $d1 = 63 $d2 = 32 $d3 = 33 $t1 = 73 $t2 = 42 $t3 = 43 Object Changed (print_r): Test Object ( [t1:private] => 73 [t2:protected] => 42 [t3] => 43 [d1:private] => 63 [d2:protected] => 32 [d3] => 33 ) Getting all class propertynames (print_r) Array ( [0] => Test */ ?> ---- Manual Page -- http://www.php.net/manual/en/function.get-object-vars.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56096 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56096&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56096&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56096&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56096&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56096&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56096&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56096&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94077) next »