# __toString extension proposal

- Thread: https://externals.io/message/27084
- Original archive: https://news-web.php.net/php.internals/27084
- Started by Marian Kostadinov on 2006-12-19T08:22:05+00:00
- Messages: 2
- 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

---

## #27084 — Marian Kostadinov — 2006-12-19T08:22:05+00:00

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

Hi,
there is one issue that I experience since I started using 5.2.0 and
especially the new correct __toString behaviour. Generally it works fine.
But sometimes I find it useful to get __toString return another object the
also has a __toString method. So my idea is - is it possible (and good idea)
to make it work in this extended manner. Here is an example to illustrate
what I mean:

class test1 {
 function __toString() { return 'i am class 1'; }
}
class test2 {
 function __toString() { return new test1; }
}
$test = new test2;
echo $test;

---

## #27086 — Etienne Kneuss — 2006-12-19T09:09:32+00:00

URL: https://externals.io/message/27084#27086 · In reply to: #27084

Hi,

you can use:
class test2 {
    public function __toString(){ return (string) new test; }
}

Marian Kostadinov wrote:
> Hi,
> there is one issue that I experience since I started using 5.2.0 and
> especially the new correct __toString behaviour. Generally it works fine.
> But sometimes I find it useful to get __toString return another object 
> the
> also has a __toString method. So my idea is - is it possible (and good 
> idea)
> to make it work in this extended manner. Here is an example to illustrate
> what I mean:
>
> class test1 {
> function __toString() { return 'i am class 1'; }
> }
> class test2 {
> function __toString() { return new test1; }
> }
> $test = new test2;
> echo $test;
>


-- 
Etienne Kneuss
http://www.colder.ch
colder@php.net

Men never do evil so completely and cheerfully as 
when they do it from a religious conviction.
-- Pascal
