[Php] "_ toString ()"

Source: Internet
Author: User
: This article describes how to use the "_ toString ()" method in php. For more information about PHP tutorials, see. We have mentioned the method for declaring the method name starting with "--" in the class (provided by PHP ),

All methods are automatically called at different times. The "_ toString ()" method is also automatically called,

It is automatically called when the object reference is directly output. we have mentioned that the 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 an error like "Catchable fatal error: Object of class Person cocould not be converted to string,

If you define the "_ toString ()" method in the class, no errors will be generated when the object is referenced directly,

Instead, the "_ toString ()" method is automatically called to output the characters returned by the "_ toString ()" method,

Therefore, the "_ toString ()" method must have a return value (return statement ).

Function _ toString () {/* is added to the session, and the subscript is stored as code */$ _ SESSION ["code"] = strtoupper ($ this-> checkCode ); // randomly generated characters exist in the session $ this-> outImg (); return '';}

The above describes the use of the [php] "_ toString ()" method, including some content, and hope to help friends who are interested in PHP tutorials.

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: info-contact@alibabacloud.com 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.