3 levels of objects

Posted on 2004-11-13
Last Modified: 2008-02-26

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?

Question by:thepadders
    LVL 2

    Accepted 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.

    Author Comment

    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?
    LVL 2

    Expert Comment

    Yeah, that makes sense to me.  Just keep the appropriate methods within the appropriate classes, and it all makes sense. :)

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now