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?
dynamic object members
Moderator: General Moderators
Re: dynamic object members
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;