Re: Re: [ZEND-ENGINE-CVS] cvs: ZendEngine2 / zend_API.c

From: Date: Mon, 01 Dec 2003 13:56:29 +0000
Subject: Re: Re: [ZEND-ENGINE-CVS] cvs: ZendEngine2 / zend_API.c
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-5874@lists.php.net to get a copy of this message
On 2003/12/01, at 19:39, Andi Gutmans wrote:
At 12:50 AM 12/1/2003 -0500, Adam Maccabee Trachtenberg wrote:
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.
__toString() is not used for automatic type casting. It is used as a convention *and* to make it nice to print objects. If you need anything more than that then call it explicitly. It would be a nightmare to modify the engine the way you are thinking and many extensions would be fixed.
Probably I didn't catch up with the discussion, but I once mentioned that automatic type casting is quite useful to implement a internationalized string container class. I think it's not only for giving human-readable string representation but it even more makes sense to me in practice. Moriyoshi

« previous php.internals (#5874) next »