Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

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?
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

First comment:
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.

// THIS CREATES A NEW POINTER (VARIABLE NAME) FOR THE SAME OBJECT
I thought a pointer was  a reference
But you wrote variable name.
Avatar of rgb192

ASKER

Second comment:
 Each such entry in the symbol table is a reference to the box location.  

Open in new window

I thought that
$x is variable
and
&$x is reference

but $x could reference box location?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

Ray's comment:
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.


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;
$x and $y are both variables which reference same box of new StdClass object.
Avatar of rgb192

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
<?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');

Open in new window


Notice: Undefined variable: emp1 in C:\Users\Acer\Documents\NuSphere PhpED\Projects\noname325.php on line 23

Notice: Undefined variable: emp1 in C:\Users\Acer\Documents\NuSphere PhpED\Projects\noname325.php on line 24

Notice: Undefined variable: emp1 in C:\Users\Acer\Documents\NuSphere PhpED\Projects\noname325.php on line 27

Fatal error: __clone method called on non-object in C:\Users\Acer\Documents\NuSphere PhpED\Projects\noname325.php on line 27
As they say in My Fair Lady, "By Jove, I Think You've Got It!

Exactly right.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

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.