The __toString method allows a class to decide how it will react when it is converted to a string.
- <html>
- <body>
- style=”color: #000000;”><?php
- // Declare a simple class
- class TestClass
- {
- public $foo;
- public function __construct($foo) {
- $this->foo = $foo;
- }
- public function __toString style=”color: #007700;”>() {
- return $this->foo;
- }
- }
- $class = new TestClass(‘Hello’);
- echo $class;
- ?>
- </body>
- </html>
The above example will output:
Hello