473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1334
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
5
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I...
21
by: Mark Rae | last post by:
Hi, Apologies if this is a stupid question - I'm a C# newbie... :-) I have a Windows app with two form: frmConfigReg and frmNewApp. frmConfigReg is the startup form. It has several controls,...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
2
by: Jerry Spence1 | last post by:
One way of passing data to a thread is to encapsulate the thread inside a class. However, I can't refer to my main form objects from within the class/thread as it says "Reference to a non-shared...
5
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
11
by: edson | last post by:
Greetings For certain operations I would like to have easy access to struct members. Here is an example. struct mystruct { char member1; char member2; char member3; };
5
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.