TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack (3) Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 09-14-2007, 04:37 PM   3 links from elsewhere to this Post. Click to view. #1 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default 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
Karl is offline  
Reply With Quote
The Following 3 Users Say Thank You to Karl For This Useful Post:
bdm (12-16-2007), Matt83 (12-15-2007), maZtah (01-10-2009)
 


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/advanced-php-programming/1117-magic-function-__tostring.html
Posted By For Type Date
PHP The Wonders of Magic Function: __toString() Tutorial This thread Refback 12-28-2007 12:08 AM
PHP - Magic Function: __toString() This thread Refback 12-26-2007 09:43 AM
PHP OOP The Wonders of Magic Function: __toString() Tutorial This thread Refback 12-22-2007 07:28 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design