3 levels of objects


Have an OOP question.

I have an object, it contains an array of objects, and each of those objects contains an array of objects.

Consider it say days, hours, minutes for example.

Now, back in the main code - I want to create the minute. What I want to do is loop throug minutes, assigning them to week, and then assigning the week to hour. How do I do that?

I know I can do something like:

for ($i = 0; $++; $i<8) {

 $day = new day();



but that dosen't seem the best way of doing it. Is there a better way, perhaps having say the week object create the day object? If so whats the best way to do that?


and have

createday() {
$day = new day();

sort of thing?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You "for" statement has its arguments mixed up.

for (startval; condition; do this after each loop)

"for ($i = 0; $++; $i<8)" becomes:
for ($i = 0; $i < 8; $i++)

Okay, now that that's taken care of... :)

Your method seems sound in concept... Remember that true OOP has all of the actions associated with the object that are relevant to that object.

So for the "week" object, you would want to do things like "addnewday" as a method within that object.  Each new week would inherit that method.

If all weeks have 7 days, I would do the loop within the constructor for the week class, and add all 7 days then.  In the constructor for the day class, I would put a loop to create 24 different hours, and within the constructor for the hour class, I'd have a loop to create all 60 minutes.

So by the simple act of creating a new week class object, you are, by default, creating 7 days, each containing 24 hours, and each hour containing 60 minutes.  The granularity is entirely up to you... wanna create 60 seconds per minute?  Do that in the minute constructor.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
thepaddersAuthor Commented:
The thing is the minute/day/hour are an example but the important thing is that the minute has to be created in the main code - thats where the data comes from. So my question is really the best way to get that object into the day class, or if i can use the day class directly to create the minute object but also get the object back in the main code.

I guess what I need to do is

$day =& $week->newDay();

class week {

var $days = array();

function newDay() {

$day =& new Day();
$this->days[] =& $day;
return $day;


Does that make sense?
Yeah, that makes sense to me.  Just keep the appropriate methods within the appropriate classes, and it all makes sense. :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.