The __toString method allows a class to decide how it will react when it is converted to a string.

  1. <html>  
  2.  <body>  
  3. style=”color: #000000;”><?php
  4. // Declare a simple class
  5. class TestClass
  6. {
  7. public $foo;
  8. public function __construct($foo) {
  9. $this->foo = $foo;
  10. }
  11. public function __toString style=”color: #007700;”>() {
  12. return $this->foo;
  13. }
  14. }
  15. $class = new TestClass(‘Hello’);
  16. echo $class;
  17. ?>
  18.  </body>   
  19. </html>  

The above example will output:

Hello
eXTReMe Tracker