Re: E_RECOVERABLE_ERROR for object to string conversion
| From: | Christoph M. Becker | Date: | Wed, 27 Sep 2017 13:01:22 +0000 |
| Subject: | Re: E_RECOVERABLE_ERROR for object to string conversion | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-100787@lists.php.net to get a copy of this message | ||
On 27.09.2017 at 14:45, Marlies Heijkoop wrote:
> Hi Internals,
>
> I stumbled upon a difference in behavior between PHP >=7.0 and HHVM when it comes to
> object-to-string conversion for objects not implementing a ³bÊW–%z¬z
> «çJSY__toString which caught me by surprise.
>
> It's probably a pretty common scenario, especially for inexperienced PHP developers, as
> all it takes is to accidentally name your method
_toString() or
> toString() rather than __toString().
>
> When trying to use an object which does not implement a __toString(), PHP will emit an
> E_RECOVERABLE_ERROR where HHVM throws an Error.
> See https://3v4l.org/WtJff for a minimal example. HHVM's
> behavior makes more sense to me here.
>
> To me, PHP's behavior is especially unexpected if you consider the behavior in
> https://3v4l.org/lF7gR where PHP *does* throw a (Type)Error because
> of the typehint. Note this example works if you change $a for an
> instance of something with a proper __toString()-method.
>
> I searched through the discussions on the original 'Exceptions in the engine' RFC and
> couldn't find any mention of this specific case so I was wondering if anyone remembers if this
> was deliberately kept this way? I'm not sure if I have the skill to write a patch, I'm
> willing to try, but it'd be a waste of time if there is some good reason for having kept this
> an E_RECOVERABLE_ERROR I'm unaware of.
I assume that this is closely related to the fact that PHP can't throw
from inside of __toString(), see <https://3v4l.org/BhPiN>
and
<http://markmail.org/message/lkpmpnhrvcbafhkd>.
--
Christoph M. Becker