__toString() fucntion in PHP5

__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.

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.