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
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack (3) Thread Tools Search this Thread Display Modes
Old 09-14-2007, 05: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: 438
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 2 Users Say Thank You to Karl For This Useful Post:
bdm (12-16-2007), Matt83 (12-15-2007)
Old 09-20-2007, 07:43 PM   #2 (permalink)
The Gregarious
Upcoming Programmer Inquisitive 
 
Join Date: Sep 2007
Posts: 748
Thanks: 85
Tanax is on a distinguished road
Default

Wow, sounds awesome!
Didn't get the $aMembers though.. where did that come from?
Tanax is offline  
Reply With Quote
Old 09-20-2007, 07:51 PM   #3 (permalink)
daz
The Contributor
Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 31
Thanks: 0
daz is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Wow, sounds awesome!
Didn't get the $aMembers though.. where did that come from?
It's hypothetical.

Useful guide, thanks Karl.
daz is offline  
Reply With Quote
Old 09-21-2007, 09:44 AM   #4 (permalink)
The Gregarious
Upcoming Programmer Inquisitive 
 
Join Date: Sep 2007
Posts: 748
Thanks: 85
Tanax is on a distinguished road
Default

Yea, I got that.. but how would he get it.. hypotheticly?
Tanax is offline  
Reply With Quote
Old 09-21-2007, 11:17 AM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 753
Thanks: 2
Salathe is on a distinguished road
Default

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";

__________________
Salathe is offline  
Reply With Quote
Old 09-24-2007, 03:42 AM   #6 (permalink)
The Visitor
 
Join Date: Sep 2007
Posts: 1
Thanks: 0
fahimilyas is on a distinguished road
Default

it is really very useful.
fahimilyas is offline  
Reply With Quote
Old 12-15-2007, 06:35 PM   #7 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 445
Thanks: 49
ReSpawN is on a distinguished road
Default

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.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Reply


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 06:08 AM.

 
     

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