Solved

what is difference between &,clone

Posted on 2014-02-19
4
272 Views
Last Modified: 2014-02-23
<?php
class Employee{
  function __construct($employee){
    $this->person=$employee;
  }
  
};
// Create the Employee named John Smith with one reference.
$x = new Employee("John Smith");

// Add another reference to John Smith (the value that $x points to).
$y = $x;
$ref=&$y;
$cloney=clone $y;
//&$andref=&$y;

// Remove $x's reference.
unset($x);

// At this point, John Smith still is in memory, because $y is still referencing it.

// Remove $y's reference
unset($y);
var_dump($ref);
var_dump($cloney);

// Now PHP cleans up and completely removes John Smith from memory.

Open in new window


output:
object(Employee)#1 (1) { ["person"]=> string(10) "John Smith" } object(Employee)#2 (1) { ["person"]=> string(10) "John Smith" }

I modified this experts exchange code

are $ref and $cloney the same

have same var_dump and are not destroyed with unset $x, $y

what is difference between &,clone
0
Comment
Question by:rgb192
4 Comments
 
LVL 15

Accepted Solution

by:
Minh Võ Công earned 167 total points
ID: 39872282
$ref=&$y; not create new object, it create a point referent to object y. change data from $ref ,data from $ y change..

$cloney=clone $y; crate new object an copy data from y to conney. change data from $cloney not change data in $y and vice

http://stackoverflow.com/questions/3611986/in-php-can-someone-explain-cloning-vs-pointer-reference
0
 
LVL 36

Assisted Solution

by:Loganathan Natarajan
Loganathan Natarajan earned 167 total points
ID: 39872555
Also read these articles,

http://www.php.net/manual/en/language.oop5.references.php
http://www.php.net/manual/en/language.oop5.cloning.php

These links will give details of difference .
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 39873081
This is a frequent conundrum for programmers who come from a PHP background in procedural programming and later try to learn object-oriented programming.  Because of that, I've written an article about it, covering the nature of variables, references, and containers.  Please read this over and see if you still have any questions.  If so, please post back and I'll be glad to help.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12310-PHP-Variables-and-References.html
0
 

Author Closing Comment

by:rgb192
ID: 39881139
thanks for objects clone and variable reference explanation.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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