__toString() fucntion in PHP5
This function is used to print the object as string. For example if you try to print the object , it will print like Object id #1.Using __toString() function we can print the objects as string
Example:
class Person {
private $name;
private $age;
function __construct($name,$age){
$this->name = $name;
$this->age = $age;
}
function __toString(){
return $this->name.”,”.$this->age;
}
}
$objPerson = new Person(”User”,”22″);
echo $objPerson;
Above example will print “User,22″, because we include the __toString(). In that __toString() function, we have to specify the values to printed while printing the object.
