为什么不能从__toString()引发异常? [英] Why it's impossible to throw exception from __toString()?

查看:1011
本文介绍了为什么不能从__toString()引发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不能从__toString()中引发异常?

Why it's impossible to throw exception from __toString()?

class a
{
    public function __toString()
    {
        throw new Exception();
    }
}

$a = new a();
echo $a;

上面的代码产生了这个:

the code above produces this:

Fatal error: Method a::__toString() must not throw an exception in /var/www/localhost/htdocs/index.php on line 12

我被指向 http://php.net/manual/en/migration52.incompatible.php 描述了这种行为,但为什么?任何理由这样做?

I was pointed to http://php.net/manual/en/migration52.incompatible.php where this behavior is described, but why? Any reasons to do that?

可能有人在这里知道这一点吗?

May be anyone here knows this?

在bug跟踪器php-dev-通常情况下只要看到手册: http://bugs.php.net/50699

At bug tracker php-dev-team as usual says nothing but see manual: http://bugs.php.net/50699

推荐答案

经过几次搜索,我发现这一点,说:

After a couple searches I found this, which says:


Johannes解释说,没有办法确保在转换到字符串期间抛出的异常会被Zend Engine 正确处理,并且这不会改变,除非大部分引擎被重写。他补充说,过去曾经讨论过这些问题,并建议Guilherme检查档案。

Johannes explained that there is no way to ensure that an exception thrown during a cast to string would be handled correctly by the Zend Engine, and that this won't change unless large parts of the Engine are rewritten. He added that there have been discussions about such issues in the past, and suggested that Guilherme check the archives.

Johannes 是PHP 5.3发布管理器,所以它可能是官方的解释,你可能会发现为什么PHP行为这个

The Johannes referenced above is the PHP 5.3 Release Manager, so it's probably as "official" an explanation as you might find as to why PHP behaves this way.

该节继续提及:


__ toString()将奇怪的是,接受 trigger_error()

所以并不是所有的错误报告在 __ toString( )

So not all is lost in terms of error reporting within __toString().

这篇关于为什么不能从__toString()引发异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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