Re: __toString()

From: Date: Mon, 15 Dec 2003 06:53:35 +0000
Subject: Re: __toString()
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to internals+get-6461@lists.php.net to get a copy of this message
On Mon, Dec 15, 2003 at 01:48:39AM -0500, George Schlossnagle wrote: > >>interface Stringable { > >> public function toString(); > >>} > >> > >>And that way extensions and user code both can decide if they want to > >>give special treatment to objects that implement Stringable. > > > >I think that's reasonable, although I think it's generally useful for > >all objects to implicitly implement 'Stringable' (in the same way that > >they all implement "Cloneable", via the __clone() method, today). > > Well, the whole discussion (from my point of view) revolved around not > wanting to cast objects to strings in arbitrary contexts, even if they > had a __toString() method. You can use an interface to signify that > not only do you implement the necessary helpers to represent yourself > as a string, but also that that _is_ the behavior that you want, That > seems useful to me (would solve Adam's problems, I think), but only if > classes don't implement that by default. I agree. If a class doesn't define a __toString() method, then the casting functionality won't be available. For the record, I'm cool with your interface proposal, too, as long as it becomes a "standard" interface (i.e. it's a stock PHP interface). -- Jon Parise (jon@php.net) :: The PHP Project (http://www.php.net/)

« previous php.internals (#6461) next »