我完全失去了这里......验证某些输入后,我创建一个Message
类的实例,并尝试将数据插入到数据库:PHP开捕致命错误:类对象无法被转换成字符串
// send message
$message = $this->model->build('message', true);
$message->insertMessage($uid, $user->user_id, $title, $message);
插入的方法很直接:
// insert a new message
public function insertMessage($to_id, $from_id, $title, $body)
{
$sql = "INSERT INTO messages (to_id, from_id, title, body, create_date) VALUES
(:to_id, :from_id, :title, :body, NOW())";
$sth = $this->db->prepare($sql);
return $sth->execute([':to_id' => $to_id, ':from_id' => $from_id, ':title' => $title, ':body' => $body]);
}
然而,在提交我结束了一个空白页和Apache错误日志说:
[Tue Jul 30 22:34:44 2013] [error] [client 127.0.0.1] PHP Catchable fatal error: Object of class framework\models\Message could not be converted to string in /var/www/p-lug/p-lug_lib/framework/models/Message.php on line 18, referer: https://p-lug.localhost/message/compose/4
第18行指向return语句,但即使我删除return
也会导致相同的错误。
我读过无数有关此错误的链接,但没有一个答案似乎适用于我的示例。 在任何时候我都试图将一个对象转换为字符串或输出结果,而用于插入其他类的类似代码完美地工作。事实上,这个代码是从另一个工作实例复制粘贴的,唯一改变的是表和数据。
我对$this
和$sth
所有变量都使用var_dump()
,所有的东西都检查出来。但在上失败。这里发生了什么?
'Message'类有什么?你可以发布吗? – vee
*但即使我删除返回它导致相同的错误。*这意味着错误是在上面的行。检查'$ this-> db'是否有效。 – Raptor