Php object-oriented _ toString () Usage Details

Source: Internet
Author: User

This article will introduce you to the usage of the php object-oriented _ toString () method. The _ toString () method is automatically called, it is automatically called when an object reference is directly output.

As we have mentioned earlier, the methods for declaring the method name "--" in the class (provided by PHP) are all automatically called for execution at different times, the "_ toString ()" method is also automatically called, which is automatically called when the object is directly referenced. We have mentioned that object reference is a pointer, for example: in "$ p = new Person ()", $ p is a reference. We cannot use echo to directly output $ p, which will output "Catchable fatal error: object of class Person cocould not be converted to string. If you define the "_ toString ()" method in the class, when you directly output the Object reference, instead of generating errors, the "_ toString ()" method is automatically called to output the characters returned in the "_ toString ()" method. Therefore, the "_ toString () the method must have a return value (return Statement ).


The Code is as follows: Copy code

<? Php
// Declare a simple class
Class TestClass
Public $ foo;

Public function _ construct ($ foo ){
$ This-> foo = $ foo;
// Define a _ toString method and add a member attribute $ foo.
Public function _ toString (){
Return $ this-> foo;

$ Class = new TestClass ('hello ');

// Directly output the object
Echo $ class;

I have been familiar with the magic method _ set in php object-oriented programming. I once introduced what is magic method. This chapter introduces another magic method _ tostring ().

_ ToString () is a convenient way to quickly obtain the string information of an object. It seems that magic methods have an "automatic" feature, such as automatic acquisition and automatic printing, __tostring () this method is automatically called when the object reference is directly output.

_ ToString ()

When debugging a program, we need to know whether the data is correct. For example, when printing an object, you can check what attributes and values the object has. If the class defines the toString method, echo can print the object body during the test, the object automatically calls the toString method defined by the class to format and output the data contained in the object.

Next we will look at an instance of _ toString ().


The Code is as follows: Copy code

<? Php

02 class Person {

03 private $ name = "";

04 function _ construct ($ name = ""){


06 $ this-> name = $ name;


08 function say (){


10 echo "Hello,". $ this-> name ."! <Br/> ";


12 function _ tostring () {// define a _ toString method in the class

13 return "Hello,". $ this-> name ."! <Br/> ";



16 $ WBlog = new Person ('wblog ');

17 echo $ WBlog; // The _ toString () method in the object is automatically called when an object reference is directly output.

18 $ WBlog-> say (); // compare it with the preceding automatic call method.


Program output:

Hello, WBlog!

Hello, WBlog!

What if the "_ tostring ()" method is not defined? For example, based on the code above, block the "_ tostring ()" method and look at the output result of the program:

Catchable fatal error: Object of class Person cocould not be converted to string

It can be seen that if the "_ tostring ()" method is not defined in the class, an error will occur when the reference to the image is directly output. In addition, _ tostring () the method body must have a return value.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.