Who can give an explanation? Reference "&"

Source: Internet
Author: User
Who can give an explanation? Reference "&"
class Test {protected $var = null;function __construct(&$var) {$var['z']=2;$this->var = &$var;$this->var['xx']=2;}}$x=['b'=>2];$d=new Test($x);print_r($x);


If the above code changes $ this-> var = & $ var to $ this-> var = $ var;
The statement xx = 2 does not take effect. $ Var isn't it referenced already?


Reply to discussion (solution)

You modified $ this-> var but referenced the temporary variable $ var of the constructor.
So $ x remains unchanged.
$ This-> var ['XX'] = 2; then change to $ var ['XX'] = 2;

You modified $ this-> var but referenced the temporary variable $ var of the constructor.
So $ x remains unchanged.
$ This-> var ['XX'] = 2; then change to $ var ['XX'] = 2;



I don't quite understand what you mean. $ This-> var does not refer to the reference of the external variable $ x? Because the $ var in the constructor is referenced. Reference this to the var in the class.

Is there any problem? Normally, it's the reference address.

class Test {    protected $var = null;    function __construct(&$var) {        $var['z']=2;        $this->var = &$var;        $this->var['xx']=2;    }}  $x=array('b'=>2);$d=new Test($x);print_r($x);


Array
(
[B] => 2
[Z] => 2
[Xx] => 2
)

$ X = array ('B' => 2 );
$ A = & $ x; // $ a is a reference of $ x.
$ A ['A'] = 'a'; // therefore, changes to $ a will affect $ x.
$ B = $ a; // $ B is not a reference of $
$ B ['B'] = 'B'; // therefore, changes to $ B will not affect $ a, nor be passed to $ x.
Print_r ($ x );

Http://www.cnblogs.com/thinksasa/p/3334492.html
You can see it.

$ This-> var = $ var; is not a reference, $ this-> var cannot be modified? Movie? $ Var
$ This-> var = & $ var; is a reference, $ this-> var modification? Movie? $ Var

I don't quite understand what you mean. $ This-> var does not refer to the reference of the external variable $ x? Because the $ var in the constructor is referenced. Reference this to the var in the class.



I seem to understand where your question is.
If $ B references $ a, you can assign a value to $ c with $ B. If $ B is not referenced again, $ c returns a new memory data, to reference $ c to $ a, $ c = & $ B is required.



I don't quite understand what you mean. $ This-> var does not refer to the reference of the external variable $ x? Because the $ var in the constructor is referenced. Reference this to the var in the class.



I seem to understand where your question is.
If $ B references $ a, you can assign a value to $ c with $ B. If $ B is not referenced again, $ c returns a new memory data, to reference $ c to $ a, $ c = & $ B is required.



After reading the manual, it seems that php and C language are not a concept at all.

The reference of php is not the same as the pointer of C language.
However, the code in your main post is the same as the equivalent c code.

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.