Re: __toString()

From: Date: Mon, 15 Dec 2003 12:13:50 +0000
Subject: Re: __toString()
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-6466@lists.php.net to get a copy of this message
Hi Andi, On Mon, Dec 15, 2003 at 02:28:41PM +0200, Andi Gutmans wrote : > We have had this discussion before. There are two extremes to this issue. > The first is to always require the explicit __toString() call and the > second is to never require it and have all places in PHP whether it's > str_replace(), (string)$obj, $arr[$obj] to automatically convert to string. > The latter is problematic because it propagates to far too many places and > causes too much magic in my opinion. > I think the right trade off is to automatically convert it with "print" and > with concatenation. All the rest will require explicit calling. > It might not be your dream but it's a realistic approach. I understand your concern about too much magic very well. But, isn't it the fact the only people who "know what they're doing" will start to take advantage of __toString ? I cannot imagine that beginners will start to implement their own __toString methods right from the start. Doesn't this limit the number of people who might shoot themselves by a great deal? So, to take advantage that any object has the string-autocast feature one needs to implement __toString. But by the time people do this, they know how it works (else why should the use __toString if they wouldn't know about its advantage [letting alone those poor people naming their methods accidantly __toString]). The option to call __toString on concatenation also will probably result in such code return $someobject . ""; instead of return $someobject->__toString(); - Markus

« previous php.internals (#6466) next »