Object Relationship Question

I'm new to OO, so please be patient. :p What is it called when one object contains another?

Like this: $calendarYear->january = new Month();

Would "$calendarYear->january" be considered a 'child object' of the "$calendarYear" object?

One more question along these lines...

Is there a way that the "$calendarYear->january" object can access the attributes of it's 'parent' object, "$calendarYear"? I wish I could do the following....


class CalendarYear
{
public $january = NULL; // Month object
public $num = 2007;

public function __construct()
{
$this->january = new Month();
}
}

class Month
{
public function showYear()
{
return $parent->num;
}
}

$calendarYear = new CalendarYear();

echo $calendarYear->january->showYear();




Where "january" can access "calendarYear->num" using $parent->num, outputting "2007". Alas, the magic "$parent" does not exist. Any advice for a OO newb?

Thanks!What is it called when one object contains another?
Composition, or aggregation if the object being contained is not owned by the other object.

Would "$calendarYear->january" be considered a 'child object' of the "$calendarYear" object?
No, $calendarYear->january is a member variable or a property or a data member (etc). It would only be an instance of a child class (or derived class, or subclass, etc) if inheritance was used. However, for this model inheritance does not seem to be the correct approach.

Where "january" can access "calendarYear->num" using $parent->num, outputting "2007".
Under your given model, showYear() would be better as a member function (a.k.a. method) of CalendarYear.
 
Back
Top