# Tip for 5.3: exceptions in __toString and __autoload

- Thread: https://externals.io/message/42373
- Original archive: https://news-web.php.net/php.internals/42373
- Started by David Grudl on 2008-12-26T01:46:43+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #42373 — David Grudl — 2008-12-26T01:46:43+00:00

URL: https://externals.io/message/42373#42373

This code throws *Fatal error*: Method test::__toString() must not throw 
an exception

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

The same problem causes throwing exceptions in function __autoload.

I think this behavior is against the logic of exceptions. Excellent 
feature of PHP 5.3 would be to resolve this errors.

David Grudl

---

## #42374 — Scott MacVicar — 2008-12-26T02:17:17+00:00

URL: https://externals.io/message/42373#42374 · In reply to: #42373

> This code throws *Fatal error*: Method test::__toString() must not  
> throw an exception
>
> class Test1
> {
>   public function __toString()
>   {
>       throw new Exception;
>   }
> }
>
> The same problem causes throwing exceptions in function __autoload.
>
> I think this behavior is against the logic of exceptions. Excellent  
> feature of PHP 5.3 would be to resolve this errors.
>
>

This is down to the design of the engine, we perform casting to  
strings in a lot of different places and it couldn't be guaranteed  
that the engine would handle any exceptions correctly.

I'm pretty sure there is something in the internals archives about  
this change.

Scott

---

## #42375 — Alexey Zakhlestin — 2008-12-26T07:14:45+00:00

URL: https://externals.io/message/42373#42375 · In reply to: #42373

> This code throws *Fatal error*: Method test::__toString() must not throw an
> exception
>
> class Test1
> {
>   public function __toString()
>   {
>       throw new Exception;
>   }
> }
>
> The same problem causes throwing exceptions in function __autoload.
>
> I think this behavior is against the logic of exceptions. Excellent feature
> of PHP 5.3 would be to resolve this errors.

regarging __autoload, everything is correct, actually, as throwing
exception doesn't let engine to try other variants (which is
counter-intuitive). it is perfectly fine to have several autoloaders
enabled.

regarding __toString(), I am not convinced that it is unresolvable.
Someone with better understanding of engine should comment


-- 
Alexey Zakhlestin
http://www.milkfarmsoft.com/

---

## #42386 — Stan Vassilev | FM — 2008-12-27T14:46:24+00:00

URL: https://externals.io/message/42373#42386 · In reply to: #42375

Hi,

With __autoload you can throw an exception as long as you define the class 
requested.
If you don't, the fatal error from the fact the class isn't there is 
triggered before the exception and you never see it.

A clutch would be eval-ing an empty class with the same name right before 
you throw. I leave up to you how appropriate that is.

Regards,
Stan Vassilev
