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

asked on

clone vs reference in php

how does
clone
differ from
reference
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg 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
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

Julian:
Because '&'  &$anothervalue will continue to be modified except for when it is cloned?

Ray:
In the Singleton design pattern you might make the __clone() method final and private to avoid accidental proliferation.

explain please

AngelIII:
=> it will not NOT change in $clonevar , except if the "something" is actually a reference itself.
how could an example of this happen?
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
Because '&'  &$anothervalue will continue to be modified except for when it is cloned?
Not sure I understand the question
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

Because '&'  &$anothervalue will continue to be modified except for when it is cloned?

Not sure I understand the question


will  &$anothervalue will continue to be modified


will  clone &$anothervalue will continue to be modified
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

When an object is cloned - the anothervalue variable is copied so the clone has its own copy.

Any change made to the value in the clone will now be independent of that made to the variable in the object that WAS cloned.

clone copies the object.
reference is just a pointer to the same object.



Thanks
After seeing this question, I wrote an article to explain and demonstrate the difference:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12310-PHP-Variables-and-References.html

Thanks for the points, ~Ray