__toString extension proposal

php.internals

Marian Kostadinov

19 years ago
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

19 years ago
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