Create Objects containing other object in PHP
Description
The following code shows how to create Objects containing other object.
Example
// www . j ava2s . c o m
<?php
class Room
{
public $name;
function __construct($name="unnamed")
{
$this->name = $name;
}
}
class House
{
//array of rooms
public $room;
}
//create empty house
$home = new house;
//add some rooms
$home->room[] = new Room("bedroom");
$home->room[] = new Room("kitchen");
$home->room[] = new Room("bathroom");
//show the first room of the house
print($home->room[0]->name);
?>
The code above generates the following result.