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.