470,821 Members | 1,968 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,821 developers. It's quick & easy.

referring to an object member

I have an object ($obj) that contains members 'sunday', 'monday',
'tuesday', 'wednesday', etc. I can refer to them in my code as
$obj->sunday, etc. But I want to refer to these members in a loop, such as:

$dow = array('sunday','monday','tuesday',...);

foreach ($dow as $day) {
print $obj->$day;
}

But this syntax does not work. I tried eval() and a few other things
with {}, but all failed.

Can I do this sort of thing? If so, what's the correct syntax?
Mark
Dec 20 '05 #1
4 1164
Hrm, why do you have each day as a seprate member variable?

I would set it up as , an example:
$obj->dayOfWeek['sunday']

dayOfWeek being an array to hold each of the separate days data.....

Dec 20 '05 #2
cross at php net wrote:
Hrm, why do you have each day as a seprate member variable?
Well, because it comes from the database that way where there are
columns named 'sunday,'monday','tuesday', etc. I can't change the database.
I would set it up as , an example:
$obj->dayOfWeek['sunday']

dayOfWeek being an array to hold each of the separate days data.....

This requires changing the database, or adding substantial code to fake
it. Is there no simpler way to refer to it similar to this: $obj->$dow
where $dow could be 'sunday, 'monday', etc.?

Mark
Dec 21 '05 #3
That is the correct syntax. It should work. Are you sure the names are
correct? Do post the result of print_r() on one of these objects.

Maybe you should consider storing your rows in associative arrays
instead. It's more obvious.

Dec 21 '05 #4
Chung Leong wrote:
That is the correct syntax. It should work. Are you sure the names are
correct? Do post the result of print_r() on one of these objects.

Thanks! Now that I have this assurance that my syntax is right, I'm
been able to determine my problem existed elsewhere, and I've fixed it.
Mark
Dec 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jeff Greenberg | last post: by
21 posts views Thread by Mark Rae | last post: by
5 posts views Thread by Frederick Gotham | last post: by
11 posts views Thread by edson | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.