PHP Catchable 致命错误:无法将类的对象转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17961347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Catchable fatal error: Object of class could not be converted to string
提问by mister martin
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:
我在这里完全迷失了......在验证了一些输入之后,我创建了一个Message
类的实例并尝试将数据插入到数据库中:
// send message
$message = $this->model->build('message', true);
$message->insertMessage($uid, $user->user_id, $title, $message);
The method for insertion is very straight-forward:
插入方法非常简单:
// 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]);
}
However, upon submission I end up with a blank page and the Apache error log says:
但是,提交后我最终得到一个空白页面,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
[Tue Jul 30 22:34:44 2013] [error] [client 127.0.0.1] PHP Catchable 致命错误:类 framework\models\Message 的对象无法转换为 /var/www/p-lug/p 中的字符串-lug_lib/framework/models/Message.php 第 18 行,引用:https://p-lug.localhost/message/compose/4
Line 18 refers to the return statement, but even if I remove return
it results in the same error.
第 18 行指的是 return 语句,但即使我删除return
它也会导致相同的错误。
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.
我已经阅读了无数关于此错误的链接,但似乎没有一个答案适用于我的示例。我绝不会尝试将对象转换为字符串或输出结果,并且与其他类插入的类似代码也能完美运行。事实上,这段代码是从另一个工作示例复制粘贴的,唯一改变的是表和数据。
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?
我已经使用var_dump()
了所有传递的变量,在$this
和 上$sth
,一切都检查出来了。但execute()
它失败了。这到底是怎么回事?
回答by Logan Murphy
So $message contains an object.
所以 $message 包含一个对象。
This object gets passed to the function insertMessage as the 4th argument ($body which is still the same object)
这个对象作为第四个参数传递给函数 insertMessage($body 仍然是同一个对象)
You then store the Message object stored in the variable $body in the hash array which is passed as an argument to execute.
然后将存储在变量 $body 中的 Message 对象存储在哈希数组中,该数组作为参数传递给执行。
The execute function attempts to convert the Message object to a string but finds that there is not __toString function declared.
execute 函数尝试将 Message 对象转换为字符串,但发现没有声明 __toString 函数。
So either declare
所以要么声明
public function __toString() {
return $the_string;
}
or create another public function/member that you can pass to the execute function.
或者创建另一个可以传递给执行函数的公共函数/成员。
回答by SGAmpere
$message
which is passed to the $body
argument is an object returned from your build()
method in the model.
$message
传递给$body
参数的是从build()
模型中的方法返回的对象。
You can simply return the message that was built in the build()
method rather than an object.
您可以简单地返回在build()
方法中构建的消息而不是对象。