Home>Article>Backend Development> What is the use of php __toString() method
"__toString()" is a magic method in PHP, which is automatically called when converting an object into a string. It is used for how a class should respond when it is treated as a string; this method must return a string , otherwise a fatal error of "E_RECOVERABLE_ERROR" level will be issued.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
__toString(), the class is treated as The response method for string
__toString() is a magic method in PHP that is automatically called when converting an object into a string.
In object-oriented programming, PHP provides a series of magic methods, which provide a lot of convenience for programming and play a very important role in PHP. Magic methods in PHP usually start with __ (two underscores) and do not need to be explicitly called but are automatically called under certain conditions.
Function:
__toString() method is used for how a class should respond when it is treated as a string. For example what `echo $obj;` should display.
Note:
This method must return a string, otherwise a fatal error of level `E_RECOVERABLE_ERROR` will be issued.
Warning:
Cannot throw exceptions in the __toString() method. Doing so will result in a fatal error.
Code:
name = $name; $this->age = $age; $this->sex = $sex; } public function __toString() { return 'go go go'; } } $person = new Person('小明'); // 初始赋值 echo $person;
Result:
go go go
So what happens if there is no __toString() magic method in the class? Let’s test it:
Code:
name = $name; $this->age = $age; $this->sex = $sex; } } $person = new Person('小明'); // 初始赋值 echo $person;
Result:
Catchable fatal error: Object of class Person could not be converted to string in D:\phpStudy\WWW\test\index.php on line 18 很明显,页面报了一个致命错误,这是语法所不允许的。
Extended information: The magic methods in PHP are as shown in the following table:
Magic method | Function |
---|---|
__construct() | Automatically called when instantiating a class |
__destruct() | Automatically called when the class object is used |
__set() | in Automatically called when assigning a value to an undefined property |
__get() | Automatically called when calling an undefined property |
__isset() | Automatically called when using isset() or empty() function |
__unset() | Automatically called when using unset() |
__sleep() | Automatically called when serializing using serialize |
__wakeup() | Use unserialize Automatically called when deserializing |
__call() | Automatically called when calling a non-existent method |
__callStatic( ) | Automatically called when calling a non-existent static method |
__toString() | Automatically called when converting an object into a string |
__invoke() | Automatically called when trying to call an object as a method |
__set_state() | When Automatically called when using the var_export() function, accepting an array parameter |
__clone() | Automatically called when using clone to copy an object |
__debugInfo() | Use var_dump() to automatically call when printing object information |
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of What is the use of php __toString() method. For more information, please follow other related articles on the PHP Chinese website!