php __toString() 使用字符串时不能抛出异常错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21104373/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:49:37  来源:igfitidea点击:

__toString() must not throw an exception error when using string

phplaravel

提问by jamespick

I am using Laravel 4 for a project I am working on. I need to retrieve the first comment from the post. I use the following code to do so.

我正在将 Laravel 4 用于我正在从事的项目。我需要从帖子中检索第一条评论。我使用以下代码来做到这一点。

$comments = Comment::where('post_id', $post->id)->first();

This successfully retrieves the first comment (I know that because I print_r-ed $commentsand it returned all the right information).

这成功地检索了第一条评论(我知道这是因为我print_r-ed$comments并且它返回了所有正确的信息)。

However, the following line of code triggers the error __toString() must not throw an exception

但是,以下代码行会触发错误 __toString() must not throw an exception

<td>{{$comments->content}}</td>

When I print_r-ed that it returned type string, and returned the correct string as well. Why then would it even try to convert $comments->contentto type string when it is already a string?

当我print_r-ed 它返回类型字符串时,并返回正确的字符串。$comments->content当它已经是一个字符串时,为什么它甚至会尝试转换为类型字符串?

回答by Drellgor

Based off the information you've given and my experience with Laravel I'd bet that the line of code causing the exception is notthe line you've put in your question.

根据您提供的信息以及我使用 Laravel 的经验,我敢打赌导致异常的代码行不是您在问题中输入的行。

<td>{{$comments->content}}</td>

This exception is complaining about the view throwing an exception. If this particular line was the issue you'd get a more descriptive exception about how $comments->content can't be converted into a string. You've also already tested that it is indeed a string.

此异常抱怨视图抛出异常。如果这一行是问题所在,您会得到一个关于 $comments->content 无法转换为字符串的更具描述性的异常。您也已经测试过它确实是一个字符串。

I'd recommend finding where your "View" object is being echoed to the view and change it like so.

我建议找到您的“视图”对象在哪里被回显到视图并像这样更改它。

{{ View::make('yourbladefile')->__tostring() }}

This worked for me by providing a more accurate and informative exception. For more info on your exception you should check out Why it's impossible to throw exception from __toString()?

这通过提供更准确和信息量更大的例外对我有用。有关您的异常的更多信息,您应该查看为什么不可能从 __toString() 抛出异常?

It's what gave me the idea in the first place. I know it's not a perfect answer so please let me know if this works and I'll update my answer if this turns out not be the case. Good luck.

这就是最初给我这个想法的原因。我知道这不是一个完美的答案,所以请告诉我这是否有效,如果事实并非如此,我会更新我的答案。祝你好运。

回答by Muriano

I know that this is an old question, but for future googlers (like me) there is another way to solve that error, and it is independent of your framework:

我知道这是一个老问题,但对于未来的谷歌人(像我一样)有另一种方法来解决这个错误,它独立于你的框架:

public function __toString() 
{
    try {
       return (string) $this->attributeToReturn; // If it is possible, return a string value from object.
    } catch (Exception $e) {
       return get_class($this).'@'.spl_object_hash($this); // If it is not possible, return a preset string to identify instance of object, e.g.
    }
}

You can use it with your custom class with no framework, or with an entity in Symfony2/Doctrine... It will work as well.

您可以将它与没有框架的自定义类一起使用,或者与 Symfony2/Doctrine 中的实体一起使用......它也可以工作。