PHP5 Tutorial – __toString() Magic Method
This tutorial will guide you through the __toString() Magic Method. The __toString() Magic method in PHP5 get called while trying to print or echo the class objects. This method can be used print all the methods and attributes defined for the class at runtime for debugging. Also this method can be used to give error message while somebody tries to print the class details.
Syntax:
< ? function __toString() { ... } ?>
Example – __toString() Magic Method Call
< ? class magicmethod { function __toString() { return "Caught You!! Cannot access the Class Object"; } } $a = new magicmethod(); echo $a; ?>
Output: Caught You!! Cannot access the Class Object
Explanation for the Example:
Here i am trying to echo the object of magicmethod class and the php compiler executes the __toString method which throws an error message.
Custom Search
Popular Articles:
- Increase Upload FileSize Limit in PHP
- PHP5 Tutorial – __autoload Magic Method
- OOPS in PHP 5 Tutorial – Parent Keyword
- Database Class in PHP5
- Exploring Magic Methods in PHP 5
- PHP 5 Tutorial – __set() Magic Method
- OOPS in PHP 5 Tutorial – Function Overriding
- PHP5 Tutorial – __isset() Magic Method
- Singleton Class in PHP 5
- OOPS in PHP 5 Tutorial – Exploring Inheritance


































