TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Magic Function: __toString() (http://www.talkphp.com/advanced-php-programming/1117-magic-function-__tostring.html)

Karl 09-14-2007 04:37 PM

Magic Function: __toString()
 
For this article we’ll be looking at the __toString magic function, and a very handy function it is to. What this function does is allow you to change the default behaviour when outputting an object instance. Consider we had the following class:

PHP Code:


class Member
{
    private 
$m_szEmail;
    private 
$m_szUsername;
    
    public function 
__construct($szUsername$szEmail)
    {
        
$this->m_szUsername $szUsername;
        
$this->m_szEmail $szEmail;
    }
}

$pMember = new Member('Karl''karl@talkphp.com');

echo 
$pMember

If we were to run this code, we would see the following output:

PHP Code:

Object 

Now let’s say throughout our project we will be listing members in the following format:

PHP Code:

Username (Email Address

Usually, most people would achieve this with code like:

PHP Code:

echo $pMember->getUsername() . " (" $pMember->getEmail() . ")"

However, what we can do is use the __toString() function to change the default behaviour of the object to string conversion. This would allow us to specify exactly what string is outputted (as we saw previously, by default, the string "Object" is outputted). To make this clearer, let’s add the following method to our class.

PHP Code:

public function __toString()
{
    return 
sprintf('%s (%s)'$this->m_szUsername$this->m_szEmail);


This method should be fairly straight forward, so I won't delve into how the code works, it basically formats and returns our string.

If we were to now execute the script again, this time, we would see the following output:

PHP Code:

Karl (karl@talkphp.com

We can then easily output a formatted list of members like so:

PHP Code:

foreach ($aMembers as $pMember)
{
    echo 
$pMember "<br />\n";


Which would output something like:

PHP Code:

Karl (karl@talkphp.com)
Wildhoney (wildhoney@talkphp.com)
Bluesage (bluesaga@talkphp.com)
Salathe (salath@talkphp.com


Tanax 09-20-2007 06:43 PM

Wow, sounds awesome!
Didn't get the $aMembers though.. where did that come from?

daz 09-20-2007 06:51 PM

Quote:

Originally Posted by Tanax (Post 2434)
Wow, sounds awesome!
Didn't get the $aMembers though.. where did that come from?

It's hypothetical.

Useful guide, thanks Karl.

Tanax 09-21-2007 08:44 AM

Yea, I got that.. but how would he get it.. hypotheticly?

Salathe 09-21-2007 10:17 AM

The $aMembers array in Karl's article could have come from anywhere, a database or hard-coded for example.

PHP Code:

$aMembers = array(
    new 
Member('Karl''karl@talkphp.com'),
    new 
Member('Wildhoney''wildhoney@talkphp.com'),
    new 
Member('Bluesaga''bluesaga@talkphp.com'),
    new 
Member('Salathe''salathe@talkphp.com')
);

foreach (
$aMembers as $pMember)
{
    echo 
$pMember "<br />\n";



fahimilyas 09-24-2007 02:42 AM

it is really very useful.

ReSpawN 12-15-2007 06:35 PM

Found this tutorial (found it AGAIN so to speak) when I was browsing Tutorialized.com.

As an explanation to Tanax, foreach ($key as $value), $key is being 'stuffed' into $value. :-)


All times are GMT. The time now is 08:51 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0