PHPconfused Posted January 20, 2009 Share Posted January 20, 2009 I need some help in writing a function that will take an object and extract properties (member variables) of that object as the array's elements I'm so confused Quote Link to comment https://forums.phpfreaks.com/topic/141611-object-extraction/ Share on other sites More sharing options...
.josh Posted January 20, 2009 Share Posted January 20, 2009 Don't really know why you're trying to accomplish this. If you have access to the object or whatever code might dynamically create the object, then you'd have access to that info anyways. But nonetheless, you can use print_r to dump out the properties of an object. If you set the 2nd argument to true, you can assign it to a variable instead of outputting the info. It will be stored in the assigned variable as a string of info (pretty much the same as serializing, only not as detailed). You can then scroll down to this user post on print_r's page, to get a nice little function that parses the print_r'ed string into an array. Quote Link to comment https://forums.phpfreaks.com/topic/141611-object-extraction/#findComment-741276 Share on other sites More sharing options...
Maq Posted January 20, 2009 Share Posted January 20, 2009 Can you post your assignment, or at least give some more detail...? Quote Link to comment https://forums.phpfreaks.com/topic/141611-object-extraction/#findComment-741318 Share on other sites More sharing options...
rhodesa Posted January 20, 2009 Share Posted January 20, 2009 http://us2.php.net/manual/en/function.get-object-vars.php Quote Link to comment https://forums.phpfreaks.com/topic/141611-object-extraction/#findComment-741324 Share on other sites More sharing options...
.josh Posted January 20, 2009 Share Posted January 20, 2009 http://us2.php.net/manual/en/function.get-object-vars.php see I was going to suggest that, except that that function is limited to the scope it's called from. So if I have for instance: <?php class something { public $a; private $b; protected $c; function __construct() { $this->a = 'apple'; $this->b = 'banana'; $this->c = 'coconut'; } // end __construct } // end class something $obj = new something; $x = print_r($obj, true); $y = get_object_vars($obj); echo "print_r<br/><pre>$x</pre>"; echo "get_object_vars<br/><pre>"; print_r($y); echo "</pre>"; ?> You would get the following: print_r something Object ( [a] => apple [b:private] => banana [c:protected] => coconut ) get_object_vars Array ( [a] => apple ) Quote Link to comment https://forums.phpfreaks.com/topic/141611-object-extraction/#findComment-741358 Share on other sites More sharing options...
DarkSuperHero Posted January 20, 2009 Share Posted January 20, 2009 Maybe you Reflection class will help you... heres a link... http://us3.php.net/oop5.reflection :-) Quote Link to comment https://forums.phpfreaks.com/topic/141611-object-extraction/#findComment-741369 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.