Page 1 of 1

dynamic object members

Posted: Sun Jan 16, 2011 4:11 pm
by cone13cone
I store locations of values in relation to its containing object(s) in a database. Question is, what is the best way to do something like below.

$var = "vendor->member->name"; // stored location in database

$invoice = new Invoice(); //I have an object that contains the vendor object and the vendor object contains the member object and so on.

So Im trying to do something like the following

$invoice->$var // which I would hope to be the same as $invoice->vendor->member->name;

any ideas?

Re: dynamic object members

Posted: Sun Jan 16, 2011 9:12 pm
by requinix
I frown on the idea (if you change your code it'll break everything, everywhere, and it won't be easy to fix) but

Code: Select all

$var = "vendor->member->name";
$invoice = new Invoice();

// ...

$value = $invoice;
foreach (explode("->", $var) as $v) $value = $value->$v;