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

后端 未结 6 1883
半阙折子戏
半阙折子戏 2020-12-29 18:33

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

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

$a = new          


        
6条回答
  •  长发绾君心
    2020-12-29 18:36

    I don't think the rationale for this decision has ever been publicized. Looks like some internal architectural limitation.

    On a more abstract level, it kind of makes sense. An object should be able to return a string representation of itself, no reason for that kind of action to fail.

提交回复
热议问题