Tag: __toString magic function

what is __toString magic function in php?

January 10, 2010 | Filed Under PHP | Comments Off

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

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

The above example will output:

Hello
Article written by admin

© PHPInterviewQuestion.com 2009 - 2012

eXTReMe Tracker