Solved

what is difference between &,clone

Posted on 2014-02-19
4
270 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
Comment Utility
$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
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
Comment Utility
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
Comment Utility
thanks for objects clone and variable reference explanation.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

8 Experts available now in Live!

Get 1:1 Help Now