• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

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?

  • 2
1 Solution
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.
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. :)

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now