PHP 5.2 released - at last a useful __toString()

I’m pretty excited about this release, there is bunch of stuff that I want to get my hands on (the Filter extention and the DateTime objects being noteworthy) but probably the most immediate change of importance to me is the fix to the magical __toString() method.

Up till 5.2 __toString() was only called when the object was sent to echo or a print() - now you can throw the object around anywhere that would cast the object to a string, and have it call the __toString() method:

[code]
// concatenation
$string = ‘foo’.$barObject;

// casting
$string = (string) $object;

// functions & language constructs
// which take strings as arguments
try
{
$foo = new Foo();
echo htmlentities($foo);
}
catch (FooException $e)
{
die($e);
}
[/code]

even the echo shortcut should work!

Thanks guys - and keep up the hard work!

Share this:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Reddit
  • Ma.gnolia
  • TwitThis

Trackback URL

No Comments on "PHP 5.2 released - at last a useful __toString()"

Hi Stranger, leave a comment:

ALLOWED XHTML TAGS:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to Comments