Jump to content

object extraction


PHPconfused

Recommended Posts

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. 

Link to comment
https://forums.phpfreaks.com/topic/141611-object-extraction/#findComment-741276
Share on other sites

 

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
)

Link to comment
https://forums.phpfreaks.com/topic/141611-object-extraction/#findComment-741358
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.