• home
  • forum
  • my
  • kt
  • download
  • The Wonders of Magic Function: __toString()

    Author: 2008-08-19 08:47:56 From:

    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

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (9)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot