Re: Re: [ZEND-ENGINE-CVS] cvs: ZendEngine2 / zend_API.c
| From: | Adam Maccabee Trachtenberg | Date: | Mon, 01 Dec 2003 05:50:41 +0000 |
| Subject: | Re: Re: [ZEND-ENGINE-CVS] cvs: ZendEngine2 / zend_API.c | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-5844@lists.php.net to get a copy of this message | ||
On Sun, 30 Nov 2003, Andi Gutmans wrote:
> At 11:59 AM 11/28/2003 -0500, Adam Maccabee Trachtenberg wrote:
> >As far as I'm concerned, if you don't want your object to be
> >auomatically cast to a string, you shouldn't provide a __toString()
> >method.
>
> Wrong. __toString() isn't supposed to work in every case the engine expects
> a string.
> You'd probably also want $obj[3] to work as a string offset?
> In this case, maybe we should rename __toString to __toPrintable, because I
> think Marcus' patch is asking for trouble.
Maybe I'm the one who is confused. What's the point of having a magic
__toString() method (or __toPrintable()) if it doesn't let the object
pretend it's a string?
You're saying it's okay to do:
class foo {
public function __toString() {
return "foo";
}
}
$obj = new foo;
print $foo;
But not:
print htmlentities($foo);
That makes absolutely no sense to me. What if __toString() returns
characters that need encoding? Or if I want to do anything else with
the output other than just print it out?
If I don't want automatic type casting to happen, I don't define
__toString(), but myToString(), which I call when I need to convert the
object to a string.
-adam
--
adam@trachtenberg.com