By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,746 Members | 1,924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,746 IT Pros & Developers. It's quick & easy.

Object property variable variable

P: n/a
If I have an object property such as $obj->property_01 = 'blah'

how do I access it using another variable

$obj->fred = '01';

e.g. echo $obj->property_$obj->fred
returns 'blah'

I've tried $obj->property_{$obj->fred}

with no luck
Feb 19 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Tue, 19 Feb 2008 17:30:21 +0100, Hugh Oxford <ar*****@fas.comwrote:
If I have an object property such as $obj->property_01 = 'blah'

how do I access it using another variable

$obj->fred = '01';

e.g. echo $obj->property_$obj->fred
returns 'blah'

I've tried $obj->property_{$obj->fred}
It's not a terrible transparent construct, you might want to avoid this.
It looks like you could be better served having a property array and
accessing it like $obj->property[$obj->fred].

OTOH though, echo $obj->{'property_'.$obj->fred} should work, I'd advise
against it.
--
Rik Wasmus
Feb 19 '08 #2

P: n/a
On Feb 19, 11:30 am, Hugh Oxford <ares...@fas.comwrote:
If I have an object property such as $obj->property_01 = 'blah'

how do I access it using another variable

$obj->fred = '01';

e.g. echo $obj->property_$obj->fred
returns 'blah'

I've tried $obj->property_{$obj->fred}

with no luck
$varName = 'property_' . (string) $obj->fred;
echo $obj->$varName
ensure the object property has public visibility.

Possibly a cleaner way is to use getter/setter methods and
call_user_func...just my opinion, though.

Feb 19 '08 #3

P: n/a
On Feb 19, 11:37 am, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
On Tue, 19 Feb 2008 17:30:21 +0100, Hugh Oxford <ares...@fas.comwrote:
If I have an object property such as $obj->property_01 = 'blah'
how do I access it using another variable
$obj->fred = '01';
e.g. echo $obj->property_$obj->fred
returns 'blah'
I've tried $obj->property_{$obj->fred}

It's not a terrible transparent construct, you might want to avoid this.
It looks like you could be better served having a property array and
accessing it like $obj->property[$obj->fred].

OTOH though, echo $obj->{'property_'.$obj->fred} should work, I'd advise
against it.
--
Rik Wasmus
I agree with Rik...especially since it seems that you might be
iterating over the object to get the values of the properties. A
container (such as an array) is a better solution.
Feb 19 '08 #4

P: n/a
Good idea

ELINTPimp wrote:
On Feb 19, 11:37 am, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
>On Tue, 19 Feb 2008 17:30:21 +0100, Hugh Oxford <ares...@fas.comwrote:
>>If I have an object property such as $obj->property_01 = 'blah'
how do I access it using another variable
$obj->fred = '01';
e.g. echo $obj->property_$obj->fred
returns 'blah'
I've tried $obj->property_{$obj->fred}
It's not a terrible transparent construct, you might want to avoid this.
It looks like you could be better served having a property array and
accessing it like $obj->property[$obj->fred].

OTOH though, echo $obj->{'property_'.$obj->fred} should work, I'd advise
against it.
--
Rik Wasmus

I agree with Rik...especially since it seems that you might be
iterating over the object to get the values of the properties. A
container (such as an array) is a better solution.
Feb 19 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.