ID: 25122
Updated by: [EMAIL PROTECTED]
Reported By: a at b dot c dot de
-Status: Open
+Status: Bogus
Bug Type: Feature/Change Request
Operating System: Any
PHP Version: 5.0.0b1 (beta1)
New Comment:
This was changed to prevent crashes. This will hopefully
be fixed in 5.1, but cannot be fixed in time for 5.0.
Sorry.
Previous Comments:
------------------------------------------------------------------------
[2004-04-06 19:36:37] bendik at infofab dot no
>From the latest change log - 18-Mar-2004
"Changed __toString() to be called automatically only
with print and echo statements. (Andi)"
I can not for the life of me figure out why this has
been done. Why should an object behave differently when
it is echoed alone and when it is concatinated?
Example:
(the class "Foo" has a __toString() method
defined.(returns "World"))
$obj = new Foo();
echo $obj; // Produces: World!
echo "Hello, " . $obj; // Produces: Hello, Object id #1
Why, oh why?
------------------------------------------------------------------------
[2003-08-17 22:17:03] a at b dot c dot de
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 this bug report at http://bugs.php.net/?id=25122&edit=1