Solved

variable and reference (later code samples I can not tell difference)

Posted on 2014-03-30
11
211 Views
Last Modified: 2014-04-01
http://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?
0
Comment
Question by:rgb192
  • 5
  • 4
  • 2
11 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 300 total points
ID: 39966248
This is really two questions, so let's take them one at a time.

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 and assigns a new pointer (variable name) to the copy.  The object that was the source for the clone is not affected in any way.  You wind up with two variable names that point to two different objects in memory.  After cloning, if you change one of the objects, you do not have any effect on the other object.

Assignment
This is the process of creating a new pointer (variable name) to an existing data element, in this case to an existing object.  When you do this, you do not get a new object, you simply get a new name for the same object.  Why would you want to do this?  I don't know - it might get confusing, so it's more of a cautionary tale than a recommended programming practice.

<?php
$obj = new stdClass;
$obj->A = 1;
$obj->B = 2;
$obj->C = 3;

// THIS CREATES A NEW POINTER (VARIABLE NAME) FOR THE SAME OBJECT
$bbj = $obj;

// THIS USES THE NEW POINTER (VARIABLE NAME) TO MUTATE THE OBJECT
unset($bbj->B);
print_r($obj); // stdClass Object ( [A] => 1 [C] => 3 )

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 300 total points
ID: 39966487
To try to answer the other part about references...

Think of each PHP variable as if it were a box.  You put data into the box, and use the contents of the box in your programs.  But how do you know where the box is located?  You actually don't know this -- you just assign a name to the box and PHP keeps track of the location of the box.

PHP has a symbol table.  Each data element that you create in your script also creates a naming element in the symbol table.  This table is how PHP associates $x with a value after an assignment statement like this:

$x = 3;

By executing that instruction, PHP creates a box containing the integer value 3, and an entry in the symbol table containing the variable name $x with the location of the box that contains the 3.

There is a minimum of one symbol table element for each variable.  When you're working with non-OOP programming, there is almost always a one:one relationship between symbol names and variable values.  But it is also possible to have more than one entry in the symbol table pointing to the same box location.  Each such entry in the symbol table is a reference to the box location.  

In PHP the equal sign is somewhat ambiguous.  Its exact function depends on the data element to the right.  If that data element is an object, PHP does not make a copy of the object (clone makes a copy).  Instead PHP makes an additional symbol table entry and now you have two variable names pointing to the same box.  When you use unset(), you "destroy" the specified variables, but in reality, PHP only removes the symbol table pointer.  Any other references to the variable in the box remain intact.
0
 

Author Comment

by:rgb192
ID: 39966737
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.
0
 

Author Comment

by:rgb192
ID: 39966749
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?
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 300 total points
ID: 39966852
This is one of the areas where PHP's history as a procedural language makes it more difficult to speak clearly about some of the fundamentals.

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;

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;
0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 33

Accepted Solution

by:
Slick812 earned 200 total points
ID: 39967334
greetings rgb192, , I will comment, maybe to help, but unfortunately, you, as most everyone, gets Confused about what the words about this "Reference" thing may mean such as (reference, pointer,  "variable name", "refers to", "points to location") and others. (I know I get confused!). Sometimes people use these two words "reference" and, "pointer" the same way, interchangeably, as if a "pointer" was same as "reference",  and others do not. Unfortunately there are Programming Word definitions for "reference" and, "pointer", , AND there is the English Dictionary Word definitions for "reference" and, "pointer". Often people (and the PHP manual) will use a "dictionary definition" for "pointer" and I may take it as a "programming definition" and vise versa, same can be said for "reference" and other words, where you understand the words as a "dictionary definition", BUT they are writing about a "programming definition", (I know I get confused!).
So if you read "reference a box location" or "it points to a box location", did they use "programming definitions" or "dictionary definitions"? ?

For ME, I have to always LOOK CLOSELY at the code written, and try to find some way to "understand" the lines of code, even then , if it's a new "programming thing" for me, I have to get on the php server and try the code, and then CHANGE the code, again , and again trying different things, Like what happens if you place 2 && as -
$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;

= = = = = = = = = = = = =
Although you can learn the way to write working code as -
$emp1 = new employee('Jay');
$emp1->setPhone("555-5555");

$refEmp1 = $emp1;// get a usable reference to affect $emp1

$independentEmp1 = clone $emp1;// get variable copy, NOT affect $emp1
$independentEmp1->setPhone("222-2222");

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


The only way That I have learned these things, is to TRY these and other code lines in several different "experiments" and change the code again and again, to get better results with less code or or more dependable code. Of course many code I try will fail, and I have to fix it, but that's how I learn. After you read this article, did you have any idea WHAT SITUATIONS IN CODE, you could use these things to have better code work?
0
 

Author Comment

by:rgb192
ID: 39968009
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.
0
 

Author Comment

by:rgb192
ID: 39968037
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39968032
As they say in My Fair Lady, "By Jove, I Think You've Got It!

Exactly right.
0
 
LVL 33

Assisted Solution

by:Slick812
Slick812 earned 200 total points
ID: 39970108
??? OK not sure how to respond to your comment with the "Notice: Undefined variable" things ? ?, , I did not really mean for you to run the code -
$refEmp1 = $emp1;  //Instead, what code work could be better if you used -
$refEmp1 = $emp1;   //instead? ?

but I only was trying to get you to see that this question about "variable and reference" can not be answered ! !
      But is just a matter how someone trys to understand the words "pointer" and "points to" and "reference" in -
"So a reference is not always a pointer but a variable points to a reference of a value"

as meaning something, or nothing, , depending on HOW your mind is using the different (and confusing) ways to define these things.
My main point is = there is a difference between talking about them, and writing code!
You may as well give answer to Ray, and close dis question.
0
 

Author Closing Comment

by:rgb192
ID: 39970850
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now