-3

I am getting fatal error-

Catchable fatal error: Object of class TV could not be converted to string in /opt/lampp/htdocs/projects/oop/index.php on line 31.

What is is wrong here?

<?php

/**
* 
*/
class TV {

    public $model= 'xyz';
    public $volume= 1;

    function volumeUp()
    {
        $this->volume++;
    }

    function volumeDown() 
    {
        $this->volume--;
    }
}
//here we create new objects
$tv_one = new TV;
$tv_two = new TV;
//
$tv_one->volumeUp();

echo $tv_one->volume;

$tv_one->model;
//error on the following line
echo $tv_one;
dbugger
  • 15,868
  • 9
  • 31
  • 33
Beauty akter
  • 331
  • 1
  • 2
  • 8

1 Answers1

0

This is a valid statement to print a value to the page:

echo $tv_one->volume;

This isn't doing anything:

$tv_one->model;

It resolves to a value, but you don't do anything with that value. So that line of code is entirely useless and can just be removed.

This doesn't make sense:

echo $tv_one;

What do you expect the object to look like on the page? You echo values, not entire structures. So, exactly as you already do above, echo the value you want:

echo $tv_one->model;
David
  • 208,112
  • 36
  • 198
  • 279