Solved

what is difference between &,clone

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

749 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