dynamic object members

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cone13cone
Forum Newbie
Posts: 24
Joined: Fri Mar 20, 2009 8:32 pm

dynamic object members

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: dynamic object members

Post 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;
Post Reply