From: a at b dot c dot de Operating system: Any PHP version: 5.0.0b1 (beta1) PHP Bug Type: Feature/Change Request Bug description: Control over typecasting objects to strings
Description: ------------ String casting for objects. When used in a string context (a simple example being "echo $object;") objects are currently cast to the string "object", which is generally less than helpful. The enhancement is to allow objects to have a method called, say, "__string" or something similar that is called on an object whenever it is used in a situation where a string is expected and returns a string (or something, like a number, that can be readily cast to a string). One could, for example, have a complex number class, with properties $r and $i, with a method like function __string() { $s = $this->r; if($this->i>=0) $s.='+'; $s.=$this->i; $s.='i'; return $s; } so that one can echo or concatenate complex numbers with impunity. Many classes already possess "toString()" methods, no doubt in part to Java's influence. These generally perform the role that I am advocating for __string(), with the difference being that the latter is implicitly called whenever necessary. Calling the new method "toString()" could be hazardous for existing scripts that already use classes with methods with this name. Backward compatibility can of course be achieved by writing one of function __string() { return $this->toString(); } function toString() { return $this->__string(); } which will give the two methods identical behaviour, except of course for the fact that toString() is an ordinary method that needs to be called explicitly. Not to be confused with the operation of serialize(); __string() is intended for e.g., human-readable string representations of an object and isn't necessarily reversible to produce the original object. A complex hierarchical object might, for example, generate XML as a result of calling its __string() method; in the example above, a complex number might have a string string representation of "-12+2.5i"; something containing binary data (e.g., an image) might return a textual description of the data's contents rather than the data itself. Needless to say, if the __string() method is absent, casting reverts to the existing "cast to 'object'" behaviour. -- Edit bug report at http://bugs.php.net/?id=25122&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=25122&r=trysnapshot4 Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=25122&r=trysnapshot5 Fixed in CVS: http://bugs.php.net/fix.php?id=25122&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=25122&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=25122&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=25122&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=25122&r=support Expected behavior: http://bugs.php.net/fix.php?id=25122&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=25122&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=25122&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=25122&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=25122&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=25122&r=dst IIS Stability: http://bugs.php.net/fix.php?id=25122&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=25122&r=gnused