News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Refulz.com:
The __toString() Method - Objects as Strings
February 09, 2012 @ 09:27:19

On the Refulz.com blog there's a recent post introducing the __toString() magic method in PHP. This handy method allows you to define how to return an object when it's referenced as a string.

We started the study of PHP magic methods by learning about __get() magic method. [...] PHP is loosely typed language and same variable can be used or referred as string, number or object. The __toString() method is called when the code attempts to treat an object like a string. This function does not accept any arguments and should return a string.

Some quick code is included showing how it works - returning a combined string made from two private class properties when the object ($obj) is echoed out. They also show multiple ways of using the method in both pre- and post-PHP 5.2.

0 comments voice your opinion now!
tostring magic method object string


blog comments powered by Disqus

Similar Posts

Martynas Jusevicius' Blog: Method overloading in PHP 5

Jani Hartikainen's Blog: Unit testing 4: Mock objects and testing code which uses the database

Padraic Brady's Blog: Mocks, Stubs, And SimpleTest Wins

Richard Davey's Blog: Interesting memory use with GD images

Think Vitamin: 9 Magic Methods for PHP


Community Events





Don't see your event here?
Let us know!


language introduction threedevsandamaybe code release library interview laravel wordpress list application podcast install community series symfony configure api unittest developer

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework