Featured Posts

Writing Website Scrapers in PHPWriting Website Scrapers in PHP This article discusses about how to write a website scraper using PHP for web site data extraction. The concepts taught can be applied and programmed in Java, C#, etc. Basically any language that has a...

Readmore

12 common programming mistakes to avoid12 common programming mistakes to avoid Programming is an art and science and like all art and science the only way to learn is from mistakes. I have made many... and I would like to share with you the mistakes that I have made over my journey...

Readmore

7 habits of highly effective freelance programmers7 habits of highly effective freelance programmers I have developed these based on my freelancing experience. Though I have discontinued freelancing, but would like to share my practices with you. These are basic practices and have been developed over...

Readmore

  • Prev
  • Next

PHP5 Tutorial - Magic Methods - __toString() method

Posted on : 25-10-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 Magic Methods, PHP5 OOPS Tutorials, Programming

3

If you're new here, you may want to subscribe to my Newsletter. Thanks for visiting!

PHP5 provides a magic method by the name of __toString() (double underscore followed by toString()) which is useful for debugging purposes.

The __toString() method is automatically called when an object in PHP5 is converted into a string for the purpose of display or concatenation.




Following is the example of the __toString() method:

<?php

class Customer {
private $firstName, $lastName, $email;

public function __construct($firstName, $lastName, $email) {
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
}

public function __toString() {
return “Debug message from Customer Class : First Name = ” . $this->firstName . “, Last Name = ” . $this->lastName . “, Email = ” . $this->email;
}
}

$c = new Customer(”Sunil”,”Bhatia”,”email@domain.com”);

echo “Customer Object is >>” . $c;

?>

Output:

Customer Object is >> Debug message from Customer Class : First Name = Sunil, Last Name = Bhatia, Email = email@domain.com

See how in this example $c Customer Object got converted into a string type when used with the dot (.) concatenation operator. In the background the magic method __toString() is automatically called when such a conversion happens.

Security Tip:
Be careful not to include sensitive data as part of the output as you could compromise security by leaking secure information. Many applications are written to write object states in a log file, therefore you should ensure that sensitive information like Credit Card information, etc is not made available through the magic method __toString()

Subscribe to my newsletter and be informed as a new PHP5 tutorial is posted online:

Your email:

 




Comments (3)

[...] Articles: PHP5 OOPS - Magic Methods __toString() PHP5 OOPS [...]

[...] Similarly, when the program calls unset() on the undeclared variable i.e. $c->name, the PHP5 magic method __unset() is invoked that takes the name of the undeclared variable i.e. ‘name’ and unsets the internal array $data[’name’]. Related Posts: PHP5 OOPS Tutorial __get() and __set() PHP5 Tutorial - Magic Methods - __toString() method [...]

[...] PHP5 Tutorial - Magic Methods - __toString() method [...]

Write a comment

Enter this code