PHP Object-oriented __tostring () usage detailed _php tutorial

Source: Internet
Author: User
This article to introduce to you about PHP object-oriented __tostring () usage, the __tostring () method is automatically called, is the direct output object reference when automatically called


As we said earlier in the class, the method of declaring "--" The Starting method name (provided by PHP to us) is automatically invoked at some point in different situations, and the "__tostring ()" method is also automatically called, which is called automatically when the object reference is directly exported. We said before that the object reference is a pointer, for example: "$p =new person ()", $p is a reference, we can not use the Echo Direct output $p, this will output "catchable fatal error:object of class person Could not being converted to string "error, if you define" __tostring () "method in the class, when the direct output object reference, will not produce an error, but automatically called the" __tostring () "Method, Output" _ The character returned in the _tostring () "method, so the" __tostring () "method must have a return value (return statement).

Code

The code is as follows Copy Code

Declare a simple class
Class TestClass
{
Public $foo;

Public function __construct ($foo) {
$this->foo = $foo;
}
Define a __tostring method and return a member property $foo
Public Function __tostring () {
return $this->foo;
}
}

$class = new TestClass (' Hello ');

Direct Output Object
Echo $class;
?>

Déjà vu, in the PHP object-oriented Programming Magic method __set, once introduced what is the Magic method, this chapter introduces a magic Method __tostring ().


__tostring () is a quick way to get the string information of an object quickly, and it seems that the magic method has an "automatic" feature, such as automatic acquisition, automatic printing, and so on, __tostring () is no exception, it is the method that is called automatically when the object reference is directly exported.


The role of __tostring ()

When we debug the program, we need to know if we can get the correct data. For example, when printing an object, look at the properties of this object, what the value is, if the class defines the ToString method, in the test, echo prints the object body, the object will automatically call its own class definition of the ToString method, formatted output this object contains data.

Let's look at an example of a __tostring ()

code as follows copy code

"!--? php

Class person{

Private $name = "";

Function __construct ($name = "") {

$this->name = $name;

"

" function say () {

"

" echo "Hello". $this->name. "
";

One}

Function __tostring () {//define a __tostring method in a class

Return "Hello,". $this->name. "!
";

+

$WBlog = new Person (' wblog ');

Echo $WBlog;//The Direct Output object reference automatically calls the __tostring () method in the object

$WBlog->say ();//Compare what is different from the automatic call above

.


Program output:

hello,wblog!

hello,wblog!


What happens if you don't define the "__tostring ()" Method? For example, on the basis of the above code, the "__tostring ()" method to screen off, and then look at the program output results:


Catchable fatal error:object of class person could not being converted to string


It can be concluded that if the "__tostring ()" method is not defined in the class, the error is generated by the direct output of a reference to the image, and a return value is required in the __tostring () method body.

http://www.bkjia.com/PHPjc/632687.html www.bkjia.com true http://www.bkjia.com/PHPjc/632687.html techarticle this article to introduce to your classmates about PHP object-oriented __tostring () usage, the __tostring () method is automatically called, is the direct output of the object reference is automatically called ...

  • 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.