# Exception::__toString() not called

- Thread: https://externals.io/message/8544
- Original archive: https://news-web.php.net/php.internals/8544
- Started by Timm Friebe on 2004-03-16T21:22:24+00:00
- Messages: 5
- 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

---

## #8544 — Timm Friebe — 2004-03-16T21:22:24+00:00

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

Hi,

Both of the following examples used to output the
Exception::__toString() output, which they don't anymore:

$ php-dev -r '$e= new Exception(); echo $e, "\n";'
Object id #1

$ php-dev -r '$e= new Exception(); echo (string)$e, "\n";'
Object id #1

Works fine for userland objects:

$ php-dev -r 'class Foo { function __toString() { return "foo"; }} echo
new Foo(), "\n";'
foo

- Timm

---

## #8547 — Marcus Boerger — 2004-03-16T22:45:46+00:00

URL: https://externals.io/message/8544#8547 · In reply to: #8544

Hello Timm, hello Andi,

i htought i had a better solution to the __toString() problem.
But then i found out why we made it so complex. The reason IIRC
was to prevent __toString() being called everywhere automatically
when it is a user function.

Havin said this the solution is very easy. We expand the signature
of cast_object to inform about exactly this - whether user space
functions may be called or not. From _convert_to_string() we set
that param to zero and from make_printable_zval we set it to 1
and inside the cast function we only need to verify the type of
the function if necessary.

Even better would be to add a flag that disallows usercode functions.
Because with such a flag we do not have a problem if a conversion
calls an internal method which then calls a userspace function which
it shouldn't.

Anid?


Tuesday, March 16, 2004, 10:22:24 PM, you wrote:

> Hi,

> Both of the following examples used to output the
> Exception::__toString() output, which they don't anymore:

> $ php-dev -r '$e= new Exception(); echo $e, "\n";'
> Object id #1

> $ php-dev -r '$e= new Exception(); echo (string)$e, "\n";'
> Object id #1

> Works fine for userland objects:

> $ php-dev -r 'class Foo { function __toString() { return "foo"; }} echo
> new Foo(), "\n";'
> foo

> - Timm




-- 
Best regards,
 Marcus                            mailto:helly@php.net

---

## #8548 — Marcus Boerger — 2004-03-16T22:52:20+00:00

URL: https://externals.io/message/8544#8548 · In reply to: #8547

> Hello Timm, hello Andi,

well i forgot to mentione a problem description: Some internal classes
copy the standard handler table. And hence their cast handler is set to
NULL. This we need to avoid. But still standard objects may not be
converted to strings everywhere.

> i htought i had a better solution to the __toString() problem.
> But then i found out why we made it so complex. The reason IIRC
> was to prevent __toString() being called everywhere automatically
> when it is a user function.

> Havin said this the solution is very easy. We expand the signature
> of cast_object to inform about exactly this - whether user space
> functions may be called or not. From _convert_to_string() we set
> that param to zero and from make_printable_zval we set it to 1
> and inside the cast function we only need to verify the type of
> the function if necessary.

> Even better would be to add a flag that disallows usercode functions.
> Because with such a flag we do not have a problem if a conversion
> calls an internal method which then calls a userspace function which
> it shouldn't.

> Anid?


> Tuesday, March 16, 2004, 10:22:24 PM, you wrote:

> > […]

> > […]

> > […]

> > […]

> > […]

> > […]



-- 
Best regards,
 Marcus                            mailto:helly@php.net

---

## #8551 — Andi Gutmans — 2004-03-17T08:45:26+00:00

URL: https://externals.io/message/8544#8551 · In reply to: #8544

The Exception class should support cast_object(). I don't see why we need 
to add extra flags.

Andi

At 11:45 PM 3/16/2004 +0100, Marcus Boerger wrote:
>Hello Timm, hello Andi,
>
>i htought i had a better solution to the __toString() problem.
>But then i found out why we made it so complex. The reason IIRC
>was to prevent __toString() being called everywhere automatically
>when it is a user function.
>
>Havin said this the solution is very easy. We expand the signature
>of cast_object to inform about exactly this - whether user space
>functions may be called or not. From _convert_to_string() we set
>that param to zero and from make_printable_zval we set it to 1
>and inside the cast function we only need to verify the type of
>the function if necessary.
>
>Even better would be to add a flag that disallows usercode functions.
>Because with such a flag we do not have a problem if a conversion
>calls an internal method which then calls a userspace function which
>it shouldn't.
>
>Anid?
>
>
>Tuesday, March 16, 2004, 10:22:24 PM, you wrote:
>
> > […]
>
> > […]
>
> > […]
>
> > […]
>
> > […]
>
> > […]
>
> > […]
>
>
>
>
>--
>Best regards,
>  Marcus                            mailto:helly@php.net

---

## #8552 — Marcus Boerger — 2004-03-17T08:49:31+00:00

URL: https://externals.io/message/8544#8552 · In reply to: #8551

Hello Andi,

the problem is that we have many classes that the handler table to behave
mostly like a default php object. We'd need to manually set the cast handler
for all of them.

marcus

Wednesday, March 17, 2004, 9:45:26 AM, you wrote:

> The Exception class should support cast_object(). I don't see why we need 
> to add extra flags.

> Andi

> At 11:45 PM 3/16/2004 +0100, Marcus Boerger wrote:
> > […]
