Solved

what is difference between &,clone

Posted on 2014-02-19
4
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 dynamically set the form action using jQuery.

729 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