【发布时间】:2017-01-31 18:08:22
【问题描述】:
这是我尝试在 PHP 类中使用的 __toString() 方法。它抛出错误“Catchable fatal error: Method Project::__toString() must return a string value in...”
但据我所知,我传递给它的所有内容都是一个字符串。我什至检查了$this->proj_id 和gettype($var) 以确认它是一个字符串,它确实是。
这是项目类...
class Project {
public $proj_id;
public $proj_num;
public $proj_name;
public function __construct($id, $num, $name){
$this->proj_id = $id;
$this->proj_num = $num;
$this->proj_name = $name;
}
public function __toString(){
echo "<table>";
echo "<tr><td>".'proj_id: '."</td><td> ".$this->proj_id." </td><t/r>";
echo "</table><br><br>";
}
}
这里是对象实例化...
$test_obj = new Project('XC2344','HKSTEST','Test Project');
echo $test_obj; //this is where the error shows up - even though it actually outputs the table with the correct value in both cells ?!
它实际上按照我的意愿输出表格和单元格以及这些单元格中的值,但随后给出错误并停止创建网页的其余部分。没看懂。
【问题讨论】:
-
return!==echo