Solved

what is difference between &,clone

Posted on 2014-02-19
4
271 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WordPress Themes 10 34
xampp tool 12 28
Form Processing in PHP 11 32
How do I change the from header when sending a mail 1 18
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…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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.

896 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

13 Experts available now in Live!

Get 1:1 Help Now