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.


Andi

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to