2

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

3
  • 2
    Your ride() method returns itself, so $tank is a reference to the $bmw object. Commented Feb 26, 2018 at 11:00
  • You can use magic method __toString, have a look at php.net/manual/en/language.oop5.magic.php#object.tostring Commented Feb 26, 2018 at 11:06
  • You have copied the code form the book, "the-essentials-of-object-oriented-php". At the Chain Method Lesson it is stated that, "In order for us to be able to perform the chaining, the methods should return the object and, since we are inside the class, the methods should return the $this keyword." You could use __toString(), call individually or set a getter method. Commented Feb 27, 2018 at 10:24

3 Answers 3

5

Your method ride returns an instance of the class Car so if you echo it you try to echo the existing class instance directly. You have two options now:

__toString() magic function

Inside of the class

function __toString() {
    return $this->tank;
}

The echo call

echo "The number of gallons left in the tank: " . $tank . "gal.";

http://php.net/manual/en/language.oop5.magic.php#object.tostring

Implement a getter function

Inside of the class

function getRemainingGallons() {
    return $this->tank;
}

The echo call

echo "The number of gallons left in the tank: " . $tank->getRemainingGallons() . " gal.";

Or edit the chained function

$tank = $bmw -> fill(10) -> ride(40) -> getRemainingGallons();

Make sure to choose a clear name for your method so that you always know what it does.

Sign up to request clarification or add additional context in comments.

1 Comment

Why not something with clearer intent like getRemainingGallons()?
1

Replace the line

$tank = $bmw -> fill(10) -> ride(40);// -> tank;

with

$bmw->fill(10);    
$bmw->ride(40);    
$tank = $bmw->tank; 

And it will work as expected.

Comments

0

You can do this two ways,

1) Directly access public variable

$tank->tank

2) Create a getter method for this like

public function getVolume() {
    return $this->tank;
}

Then access this by following way

echo "The number of gallons left in the tank: " . $tank->getVolume() . " gal.";<br>
echo "The number of gallons left in the tank: " . $tank->tank . " gal.";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.