可恢复的致命错误:第16行的C:\ xampp \ htdocs \ learn \ index.php中无法将类newclass的对象转换为字符串

问题描述 投票:1回答:2

我刚刚开始使用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";
    }
}
php
2个回答
1
投票

toString方法应该命名为__toString,因为这是php在尝试将对象表示为字符串时将查找的方法。


0
投票

请参阅有关魔术方法的文档 PHP Magic Methods __toString


public function toString() {}

public function __toString() {}
© www.soinside.com 2019 - 2024. All rights reserved.