I'm learning PHP OOP, but now I'm stuck with an error and confused regarding the chained method. Here is my code
<?php
class Car {
public $tank;
public function fill($float) {
$this-> tank += $float;
return $this;
}
public function ride($float) {
$miles = $float;
$gallons = $miles/50;
$this-> tank -= ($gallons);
return $this;
}
}
$bmw = new Car();
$tank = $bmw -> fill(10) -> ride(40);// -> tank;
echo "The number of gallons left in the tank: " . $tank . " gal.";
?>
Now the problem is when used to call the functions without calling the Public variable tank then it's showing the following error message.
Catchable fatal error: Object of class Car could not be converted to string in C:\xampp\htdocs\oop\chain.php on line 33
In this case why should I call the public variable tank while I am calling the two functions? If I'm not assigning any value directly to the Public Variable tank then why should I call that variable..??
I am very confused with this
ride()method returns itself, so$tankis a reference to the$bmwobject.__toString, have a look at php.net/manual/en/language.oop5.magic.php#object.tostring