Php reference returns function & fun (); learning Notes

Source: Internet
Author: User

Example

The code is as follows: Copy code

<? Php
Class talker {

Private $ data = 'hi ';

Public function & get (){
Return $ this-> data;
        }
      
Public function out (){
Echo $ this-> data;
        }  

    }

$ Aa = new talker ();
$ D = & $ aa-> get ();

$ Aa-> out ();
$ D = 'who ';
$ Aa-> out ();
$ D = 'all ';
$ Aa-> out ();
$ D = 'you ';
$ Aa-> out ();
// The output is "HiHowAreYou"
?>

Example 2:

The code is as follows: Copy code

<? Php
Class person {
Private $ name;
Function & getName (){
Return $ this-> name;
}
}

$ P = new person ();
// Note the getName in the person class. No matter whether it is added or not, no error is reported during this call. However, this call does not make any sense.
$ Name = & $ p-> getName ();
$ Name = 'Walker ';
$ Name = $ p-> getName ();
Echo $ name; // output walker


Supplement: Function Reference and return

The code is as follows: Copy code

<? Php
Function & test ()
{
Static $ B = 0; // declare a static variable
$ B = $ B + 1;
Echo $ B;
Return $ B;
}

$ A = test (); // This statement outputs the value of $ B as 1.
$ A = 5;
$ A = test (); // This statement outputs the value of $ B to 2.

$ A = & test (); // This statement outputs the value of $ B to 3.
$ A = 5;
$ A = test (); // This statement outputs a value of 6 for $ B.
?>

The following explains:

In this way, $ a = test (); is not actually returned by the function reference, which is no different from the normal function call. The reason is: this is the PHP rule.
PHP requires that $ a = & test (); is used to obtain the function reference and return.
As for what is reference return (in the PHP Manual, reference return is used when you want to use a function to find the variable on which the reference should be bound .) I haven't understood this sentence for a long time.

The example above is as follows:
$ A = test () is used to call a function. It only assigns the value of the function to $ a. Any change made to $ a does not affect $ B in the function.
But how to call a function through $ a = & test, the function is to direct the memory address of the $ B variable in return $ B to the same place as the memory address of the $ a variable.
That is, the equivalent effect ($ a = & $ B;) is generated. Therefore, changing the value of $ a also changes the value of $ B.
$ A = & test ();
$ A = 5;
Later, the value of $ B is changed to 5.

Static variables are used to help you understand the reference and return functions. In fact, function reference and return are mostly used in objects.

Another official php example is provided:

The code is as follows: Copy code

This is the way how we use pointer to access variable inside the class.

<? Php
Class talker {

Private $ data = 'hi ';

Public function & get (){
Return $ this-> data;
    }
  
Public function out (){
Echo $ this-> data;
    }  

}

$ Aa = new talker ();
$ D = & $ aa-> get ();

$ Aa-> out ();
$ D = 'who ';
$ Aa-> out ();
$ D = 'all ';
$ Aa-> out ();
$ D = 'you ';
$ Aa-> out ();
?>

The output is "HiHowAreYou"

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.