tags:

views:

33

answers:

3

How would you go about converting:

$Object->one;
$Object->two;

to:

$one;
$two;
+2  A: 

Cast it into an array then extract() it.

Keep alcuadrado's comment in mind regarding encapsulation; it's emphasized by the fact that extract() will only work on public instance variables (I've updated my example code to show this).

class TestClass
{
    public $one = 1;
    public $two = 2;
    private $three = 3;
}

$object = new TestClass;
extract((array) $object);

var_dump($one, $two, $three);

Output:

Notice: Undefined variable: third in...
int(1)
int(2)
NULL
BoltClock
+2  A: 

If you use a foreach loop on an object, it will iterate over the visible properties without exposing private members.

foreach ($object as $key => $value){
  $$key = $value;
}
mwotton
+2  A: 

Try: extract(get_object_vars($Object));

That will only get the public variables. If you want private, then you'd need to call it from within the object itself.

konforce