【发布时间】:2011-01-26 15:26:22
【问题描述】:
为什么不能从 __toString() 抛出异常?
class a
{
public function __toString()
{
throw new Exception();
}
}
$a = new a();
echo $a;
上面的代码产生了这个:
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 描述了这种行为,但为什么呢?有什么理由这样做吗?
这里有人知道吗?
在错误跟踪器 php-dev-team 中,除了查看手册之外什么也没说:http://bugs.php.net/50699
【问题讨论】:
-
PHP 7.4 现在 allows exceptions 在
__toString()