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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.