I have two classes. Truck and Tire. Tire has (1) size and (2) tread. Truck has (1) make, (2) model and (3) Tire. I am having trouble after assigning an instance of Tire to an instance of Truck. Consider the following code. Can anyone help?
Code:
class Truck {
private $make, $model, $tire;
function __construct($ma, $mo) {
$this->make = $ma;
$this->model = $mo;
}
public function assignTire($t) {
$this->tire = $t;
}
public function toString() {
if ($this->tire != NULL && $this->tire != "") {
// --- [b]PROBLEM HAPPENS HERE[/b] ---
return "$this->make $this->model $this->tire->toString()"; // [b]results in ERROR[/b]
//return "$this->make $this->model (Tire)$this->tire->toString()"; // [b]results in ERROR[/b]
//return "$this->make $this->model (Tire)($this->tire)->toString()"; // [b]results in ERROR[/b]
}
return "$this->make $this->model";
}
}
class Tire {
private $size, $tread;
function __construct($s, $t) {
$this->size = $s;
$this->tread = $t;
}
public function toString() {
return "$this->size $this->tread";
}
}
$myTruck = new Truck ("Toyota", "Tacoma"); // Create instance of Truck object.
echo $myTruck->toString() . "<br />\n"; // [COLOR=blue]Prints "Toyota Tacoma"[/color]
$stockTire = new Tire ("P265/70R16", "All Terrain"); // Create instance of Tire object.
echo $stockTire->toString() . "<br />\n"; // [COLOR=blue]Prints "P265/70R16 All Terrain"[/color]
$myTruck->assignTire($stockTire); // Assign instance of Tire object to $myTruck.
echo $myTruck->toString() . "<br />\n"; // [COLOR=blue][b]ERROR!![/b][/color]