PHP可捕获的致命错误:类的对象无法转换为字符串 [英] PHP Catchable fatal error: Object of class could not be converted to string

查看:307
本文介绍了PHP可捕获的致命错误:类的对象无法转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里完全迷路了……验证了一些输入后,我创建了一个Message类的实例,并尝试将数据插入数据库:

I am completely lost here... After validating some input I create an instance of a Message class and attempt to insert data into the database:

// 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错误日志显示:

However, upon submission I end up with a blank page and the Apache error log says:

[2013年7月30日星期二22:34:44] [错误] [客户端127.0.0.1] PHP可捕获 致命错误:不能是类framework \ models \ Message的对象 转换成字符串 /var/www/p-lug/p-lug_lib/framework/models/Message.php,第18行, 引荐来源: https://p-lug.localhost/message/compose/4

[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,它也会导致相同的错误.

Line 18 refers to the return statement, but even if I remove return it results in the same error.

我已经阅读了无数有关此错误的链接,但似乎没有答案适用于我的示例. 我绝对不会尝试将对象转换为字符串或输出结果,并且类似的代码可完美插入其他类.实际上,此代码是从另一个工作示例复制粘贴的,唯一更改的是表和数据.

I've read countless links regarding this error but none of the answers appear to apply to my example. At no point am I trying to convert an object to a string or output the result, and similar code for insertions with other classes works perfectly. In fact, this code was copy-pasted from another working example, the only thing changed is the table and data.

我在所有要传递的变量上使用了var_dump(),在$this$sth上,所有内容都已签出.但是在execute()上它失败.到底发生了什么事?

I've used var_dump() on all the variables being passed, on $this and $sth, everything checks out. But on execute() it fails. What the heck is going on here?

推荐答案

因此$ message包含一个对象.

So $message contains an object.

该对象作为第四个参数($ body仍然是同一对象)传递给函数insertMessage.

This object gets passed to the function insertMessage as the 4th argument ($body which is still the same object)

然后,将存储在变量$ body中的Message对象存储在哈希数组中,该哈希对象作为要执行的参数传递.

You then store the Message object stored in the variable $body in the hash array which is passed as an argument to execute.

execute函数尝试将Message对象转换为字符串,但是发现没有声明__toString函数.

The execute function attempts to convert the Message object to a string but finds that there is not __toString function declared.

所以要么声明

public function __toString() {
    return $the_string;
}

或创建另一个公共函数/成员,您可以将其传递给执行函数.

or create another public function/member that you can pass to the execute function.

这篇关于PHP可捕获的致命错误:类的对象无法转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆