Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this error shows? Object of class Hide could not be converted to string

Tags:

php

i have this code

class  Hide {

    private $myname;
    function getmyname()
    {
        $myname = __class__;
        return $myname;
    }
}

class  damu {
    private static $name;
    public function name()

    {
    var_dump($this->name);
        if( $this->name == null ){
               $this->name = new Hide();
          }
          return $this->name;
    }
}

$run = new damu();
echo $run->name();

this giving me an error

Catchable fatal error: Object of class Hide could not be converted to string

what is the meaning of this and how to resolve this.

like image 409
user439555 Avatar asked Dec 13 '22 12:12

user439555


2 Answers

You're trying to echo a Hide() object, which PHP doesn't know how to convert to a string. This is due to the following lines:

        if( $this->name == null ){
           $this->name = new Hide();
      }
      return $this->name;

and then

echo $run->name();

Instead of echo, try

print_r($run->name());
like image 112
Christopher Armstrong Avatar answered Dec 15 '22 01:12

Christopher Armstrong


You return an instance of Hide and try to echo it. Since your implementation does not have a __toString() method, there is no string representation and you get that error. Try this:

$run = new damu();
echo $run->name()->getmyname();

or add a __toString() method to Hide.

like image 36
cem Avatar answered Dec 15 '22 01:12

cem