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.
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());
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With