Home > Backend Development > PHP Problem > What is the use of php __toString() method

What is the use of php __toString() method

青灯夜游
Release: 2023-03-10 18:20:01
Original
2464 people have browsed it

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

What is the use of php __toString() method

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:

<?php
class Person
{
    public $sex;
    public $name;
    public $age;
    public function __construct($name="",  $age=25, $sex=&#39;男&#39;)
    {
        $this->name = $name;
        $this->age  = $age;
        $this->sex  = $sex;
    }
    public function __toString()
    {
        return  &#39;go go go&#39;;
    }
}
$person = new Person(&#39;小明&#39;); // 初始赋值
echo $person;
Copy after login

Result:

go go go
Copy after login

So what happens if there is no __toString() magic method in the class? Let’s test it:

Code:

<?php
class Person
{
    public $sex;
    public $name;
    public $age;
    public function __construct($name="",  $age=25, $sex=&#39;男&#39;)
    {
        $this->name = $name;
        $this->age  = $age;
        $this->sex  = $sex;
    }
    
}
$person = new Person(&#39;小明&#39;); // 初始赋值
echo $person;
Copy after login

Result:

Catchable fatal error: Object of class Person could not be converted to string in D:\phpStudy\WWW\test\index.php on line 18
很明显,页面报了一个致命错误,这是语法所不允许的。
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template