我刚刚开始使用PHP进行编程,现在我遇到了这个错误
可恢复的致命错误:第16行的C:\ xampp \ htdocs \ learn \ index.php中无法将类新类的对象转换为字符串
<?php
class newclass
{
public $info = "hey there";
public $err = "this is the class called ".__CLASS__."!";
public function __construct()
{
echo "this class has been instantiated";
}
public function toString()
{
echo new render();
return $this->err;
}
public function conn($name)
{
$this->info = $name;
}
public function getdata()
{
return $this->info;
}
public function __destruct()
{
echo "this is the end of class";
}
}
toString方法应该命名为__toString,因为这是php在尝试将对象表示为字符串时将查找的方法。
请参阅有关魔术方法的文档 PHP Magic Methods __toString
public function toString() {}
变
public function __toString() {}