rgb192
asked on
variable and reference (later code samples I can not tell difference)
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12310-PHP-Variables-and-References.html
I am okay with beginning code samples separating variable and reference, but then i get lost especially with last code sample 'cloning' Which part of the code 'cloning is variable and which is reference? Is $x reference and $y variable?
From 'Variable Un-Assignment' going down I do not understand difference variable and reference
Which code samples are variable and which are reference?
I am okay with beginning code samples separating variable and reference, but then i get lost especially with last code sample 'cloning' Which part of the code 'cloning is variable and which is reference? Is $x reference and $y variable?
From 'Variable Un-Assignment' going down I do not understand difference variable and reference
Which code samples are variable and which are reference?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Second comment:
$x is variable
and
&$x is reference
but $x could reference box location?
Each such entry in the symbol table is a reference to the box location.
I thought that $x is variable
and
&$x is reference
but $x could reference box location?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ray's comment:
$x an $y are both variables which currently reference 2 boxes. Each box contains 3. But both can be changed independently.
Every variable name is a reference to a variable value (the information in the box) but not every use of the variable is done by reference. For example, this procedural code creates two independent variables. You can change $x and $y independently.
$x = 3;
$y = $x;
$x an $y are both variables which currently reference 2 boxes. Each box contains 3. But both can be changed independently.
$x and $y are both variables which reference same box of new StdClass object.
This object-oriented code creates a variable (box with object inside) and two references to it. You can use either name to change the object; the other name also refers to the changed variable.
$x = new StdClass;
$y = $x;
ASKER
Slick812 comment:
So a reference is not always a pointer but a variable points to a reference of a value?
until $c += 3;
$a,$b,$c are 10
Notice: Undefined variable: emp1 in C:\Users\Acer\Documents\Nu Sphere PhpED\Projects\noname325.p hp on line 23
Notice: Undefined variable: emp1 in C:\Users\Acer\Documents\Nu Sphere PhpED\Projects\noname325.p hp on line 24
Notice: Undefined variable: emp1 in C:\Users\Acer\Documents\Nu Sphere PhpED\Projects\noname325.p hp on line 27
Fatal error: __clone method called on non-object in C:\Users\Acer\Documents\Nu Sphere PhpED\Projects\noname325.p hp on line 27
So a reference is not always a pointer but a variable points to a reference of a value?
until $c += 3;
$a,$b,$c are 10
<?php
$a = 5;
$b = &$a;
$b += 2;
///= = = = = = = = = = =
//or a ref to a ref -
$a = 5;
$b = &$a;
$b += 2;
$c = &$b;// $c is a reference to a reference
$c += 3;
//= = = = = = = = = = =
class employee{
public $employee;
public function __construct($employee){
$this->employee=$employee;
}//end construct
}//end class
//IT IS MORE IMPORTANT, to learn WHY (for better code work) you would ever
//have NOT used the code line -
$refEmp1 = new employee('Jay');
//BUT use this line -
$refEmp1 = $emp1; //Instead, what code work could be better if you used -
$refEmp1 = $emp1; //instead? ?
//= = = = = = = = = = = = =
//and what code work would be better if you used -
$independentEmp1 = clone $emp1;
//instead of -
$independentEmp1 = new employee('Jay');
Notice: Undefined variable: emp1 in C:\Users\Acer\Documents\Nu
Notice: Undefined variable: emp1 in C:\Users\Acer\Documents\Nu
Notice: Undefined variable: emp1 in C:\Users\Acer\Documents\Nu
Fatal error: __clone method called on non-object in C:\Users\Acer\Documents\Nu
As they say in My Fair Lady, "By Jove, I Think You've Got It!
Exactly right.
Exactly right.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So a reference is not always a pointer but a variable points to a reference of a value?
I understand alittle better the difference
Thanks both.
I understand alittle better the difference
Thanks both.
ASKER
Cloning
This is the process of replicating an object as it exists at the moment when the clone keyword is used in the script.. In other words, it creates a copy of an existing object $REFERENCE and assigns a new pointer $VARIABLE (variable name) to the copy.
I thought a pointer was a reference
But you wrote variable name.