Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having Trouble with a simple concept involving Classes .. Plz Help 1

Status
Not open for further replies.

twofive

Programmer
Jan 9, 2007
27
US
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]
 
you do not specify the error you receive. but i suspect that this code would work

Code:
public function toString() {
	if (!empty($this->tire)) {
	    $string =  sprintf('%s %s %s', $this->make, $this->model, $this->tire->toString()); 
	} else {
        $string = sprintf('%s %s', $this->make, $this->model);
    }
	return $string;
}
 
That works! The error I was encountering was "Catchable fatal error: Object of class Tire could not be converted to string". When I attempted to manually cast using "(Tire)$this->tire->toString()" or "(Tire)($this->tire)->toString()" I recieved was "Parse error: syntax error, unexpected T_VARIABLE" for the former and "Parse error: syntax error, unexpected '('" for the latter.

I modified what you provided as follows (this works too):
Code:
public function toString() {
	if (!empty($this->tire)) {
		return $this->make . " " . $this->model . " " . $this->tire->toString();
	}
	return $this->make . " " . $this->model;
}

Thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top