Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Romanian Russian Spanish Turkish Other
Creating a copy of an object with fully replicated properties is not always the wanted behavior. A good example of the need for copy constructors, is if you have an object which represents a GTK window and the object holds the resource of this GTK window, when you create a duplicate you might want to create a new window with the same properties and have the new object hold the resource of the new window. Another example is if your object holds a reference to another object which it uses and when you replicate the parent object you want to create a new instance of this other object so that the replica has its own separate copy.
An object copy is created by using the clone keyword (which calls the object's __clone() method if possible). An object's __clone() method cannot be called directly.
When an object is cloned, PHP will perform a shallow copy of all of the object's properties. Any properties that are references to other variables will remain references.
__clone ( void ) : void
Once the cloning is complete, if a __clone() method is defined, then the newly created object's __clone() method will be called, to allow any necessary properties that need to be changed.
Example #1 Cloning an object
publicfunction
publicfunction
class
function
print(
print(
The above example will output:
PHP 7.0.0 introduced the possibility to access a member of a freshly cloned object in a single expression:
Example #2 Access member of freshly cloned object
format('Y');?>
The above example will outputsomething similar to:
8 years ago
private
}
13 years ago
I think it's relevant to note that __clone is NOT an override. As the example shows, the normal cloning process always occurs, and it's the responsibility of the __clone method to "mend" any "wrong" action performed by it.
12 years ago
public function __clone() { foreach ($this->varName as &$a) { foreach ($a as &$b) { $b = clone $b; } }}
Note, that I was working with a multi-dimensional array and I was not using the Key=>Value pair system, but basically, the point is that if you use foreach, you need to specify that the copied data is to be accessed by reference.
11 years ago
function
}
9 years ago
Another gotcha I encountered: like __construct and __desctruct, you must call parent::__clone() yourself from inside a child's __clone() function. The manual kind of got me on the wrong foot here: "An object's __clone() method cannot be called directly."
4 years ago
foreach (
public function
class
public function
echo
echo
3 years ago
public static
public function
public static function
public function
echo
unset(
echo
9 years ago
1. PHP treats variables as either 'values types' or 'reference types', where the difference is supposed to be transparent. Object cloning is one of the few times when it can make a big difference. I know of no programmatic way to tell if a variable is intrinsically a value or reference type. There IS however a non-programmatic ways to tell if an object property is value or reference type:
unset($ref);var_dump($a);
?>I interpret this as the reference-count jumping from 2 straight to 0. However...
2. It IS possible to create a reference with a reference count of 1 - i.e. to convert an property from value type to reference type, without any extra references. All you have to do is declare that it refers to itself. This is HIGHLY idiosyncratic, but nevertheless it works. This leads to the observation that although the manual states that 'Any properties that are references to other variables, will remain references,' this is not strictly true. Any variables that are references, even to *themselves* (not necessarily to other variables), will be copied by reference rather than by value.
Here's an example to demonstrate:
class ByVal{ var $prop;}
class ByRef{ var $prop; function __construct() { $this->prop =& $this->prop; }}
$a = new ByVal;$a->prop = 1;$b = clone $a;$b->prop = 2;
$a = new ByRef;$a->prop = 1;$b = clone $a;$b->prop = 2;
?>
9 months ago
public function
2 years ago
To illustrate this process, the following example codes seems better, comparing the the original version:
class SubObject{ static $num_cons = 0; static $num_clone = 0;
public $construct_value; public $clone_value;
public function __construct() { $this->construct_value = ++self::$num_cons; }
public function __clone() { $this->clone_value = ++self::$num_clone; }}
class MyCloneable{ public $object1; public $object2;
function __clone() { // this->object $this->object1 = clone $this->object1; }}
$obj = new MyCloneable();
$obj->object1 = new SubObject();$obj->object2 = new SubObject();
$obj2 = clone $obj;
print("Original Object:n");print_r($obj);echo '
';print("Cloned Object:n");print_r($obj2);==================
the output is as below
Original Object:MyCloneable Object( [object1] => SubObject Object ( [construct_value] => 1 [clone_value] => )
[object2] => SubObject Object ( [construct_value] => 2 [clone_value] => )
)
Cloned Object:MyCloneable Object( [object1] => SubObject Object ( [construct_value] => 1 [clone_value] => 1 )[object2] => SubObject Object ( [construct_value] => 2 [clone_value] => )
)
11 years ago
Keep in mind that since PHP 5.2.5, trying to clone a non-object correctly results in a fatal error, this differs from previous versions where only a Warning was thrown.
3 years ago
I believe the two functions are not quite the same. The serialize followed by deserialize method is the way I've done deep cloning in other languages (bypasses any weird clone function behavior and ensures you have a no-strings-attached copy of the object).
10 years ago
$val) { if(is_object($val)||(is_array($val))){ $this->{$key} = unserialize(serialize($val)); } }}?>That will insure any object, or array that may potentially contain objects, will get cloned without using recursion or other support methods.
[EDIT BY danbrown AT php DOT net: An almost exact function was contributed on 02-DEC-2008-10:18 by (david ashe AT metabin):
$value){ if(gettype($value)=='object'){ $this->$name= clone($this->$name); } } }?>Giving credit where it's due. ~DPB][EDIT BY cmb AT php DOT net: the latter function fails to make deep copies of object arrays, and might end up with infinite recursion.]
Follow this link:
- What is Cloning? - Learn Genetics [Last Updated On: December 12th, 2016] [Originally Added On: December 12th, 2016]
- Pros and Cons of Cloning - Buzzle [Last Updated On: December 21st, 2016] [Originally Added On: December 21st, 2016]
- Cloning/Embryonic Stem Cells - National Human Genome Research ... [Last Updated On: December 22nd, 2016] [Originally Added On: December 22nd, 2016]
- Is human cloning wrong? | Debate.org [Last Updated On: December 25th, 2016] [Originally Added On: December 25th, 2016]
- Cloning - Science Daily [Last Updated On: December 29th, 2016] [Originally Added On: December 29th, 2016]
- Cloning - The New York Times [Last Updated On: January 4th, 2017] [Originally Added On: January 4th, 2017]
- Mammoth - Wikipedia [Last Updated On: January 24th, 2017] [Originally Added On: January 24th, 2017]
- Molecular Cloning: Basics and Applications | Protocol [Last Updated On: January 25th, 2017] [Originally Added On: January 25th, 2017]
- Beware Of 'Facebook Cloning' | 9news.com - 9NEWS.com [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- Facebook scam lets hackers clone your account and STEAL money ... - Express.co.uk [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- Beware of 'Facebook Cloning' | KGW.com - kgw.com [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- Beware of Facebook 'cloning' scam - USA TODAY [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- Facebook cloning debunked - The i newspaper online iNews - iNews [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- Don't fall for this Facebook cloning scam | WFLA.com - WFLA [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- 20 years after Dolly the sheep, human cloning is no closer - Genetic Literacy Project [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- Don't fall for this Facebook cloning scam | WDTN - WDTN [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- This Crab Clones Its Allies by Ripping Them in Half - The Atlantic [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- Boffins create quantum cloning machine to intercept 'secure' messages - The INQUIRER [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- Hair Cloning & Multiplication | Bernstein Medical [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- Crustacean Cloning - ScienceBlog.com (blog) [Last Updated On: February 8th, 2017] [Originally Added On: February 8th, 2017]
- Watch out for this crazy Facebook cloning scam! - Komando [Last Updated On: February 8th, 2017] [Originally Added On: February 8th, 2017]
- Steve Bannon's Unproduced Movie About Cloning, Nazis, and Walt Disney Sounds Nuts - Gizmodo India [Last Updated On: February 9th, 2017] [Originally Added On: February 9th, 2017]
- Quantum Cloning Machine Reveals Clues That Could Protect Against Hacking - Photonics.com [Last Updated On: February 9th, 2017] [Originally Added On: February 9th, 2017]
- Steve Bannon wanted to make a movie about cloning, abortion, and Nazis with Mel Gibson - A.V. Club (blog) [Last Updated On: February 9th, 2017] [Originally Added On: February 9th, 2017]
- Police investigating recent reports of credit card cloning in Aiken ... - Aiken Standard [Last Updated On: February 10th, 2017] [Originally Added On: February 10th, 2017]
- Steve Bannon's Unproduced Movie About Cloning, Nazis, and Walt ... - Gizmodo [Last Updated On: February 10th, 2017] [Originally Added On: February 10th, 2017]
- Phone cloning - Wikipedia [Last Updated On: February 11th, 2017] [Originally Added On: February 11th, 2017]
- Gang arrested for cloning debit cards, stealing money - The Hindu - The Hindu [Last Updated On: February 11th, 2017] [Originally Added On: February 11th, 2017]
- Drive cloning in Windows 10 with free tools - Computerworld [Last Updated On: February 12th, 2017] [Originally Added On: February 12th, 2017]
- Cloning - The Hastings Center [Last Updated On: February 12th, 2017] [Originally Added On: February 12th, 2017]
- Scientists Are Close to Cloning a Woolly Mammoth - Popular Mechanics [Last Updated On: February 16th, 2017] [Originally Added On: February 16th, 2017]
- Antiquities Minister inaugurates first Pharaonic cloning center in Luxor - Egypt Independent [Last Updated On: February 18th, 2017] [Originally Added On: February 18th, 2017]
- 20 years after Dolly: Everything you always wanted to know about ... - Source [Last Updated On: February 18th, 2017] [Originally Added On: February 18th, 2017]
- 20 years after Dolly: Everything you always wanted to know about the cloned sheep and what came next - New Delhi Times [Last Updated On: February 20th, 2017] [Originally Added On: February 20th, 2017]
- 20 years after Dolly: Everything you always wanted to know about ... - The Conversation US [Last Updated On: February 20th, 2017] [Originally Added On: February 20th, 2017]
- Must reads: Populism, sexism, cloning, and rudeness - GlobalComment.com [Last Updated On: February 20th, 2017] [Originally Added On: February 20th, 2017]
- More lessons from Dolly the sheepis a clone really born at age ... - Phys.Org [Last Updated On: February 20th, 2017] [Originally Added On: February 20th, 2017]
- 15 Animals That Have Been Successfully Cloned by Scientists - Interesting Engineering [Last Updated On: February 21st, 2017] [Originally Added On: February 21st, 2017]
- Facebook does it again. WhatsApp launches revamped Status, cloning Snapchat - Catch News [Last Updated On: February 22nd, 2017] [Originally Added On: February 22nd, 2017]
- It's Been 20 Years Since We Cloned A Sheep. Why Haven't We Done The Same With Humans? - GOOD Magazine [Last Updated On: February 22nd, 2017] [Originally Added On: February 22nd, 2017]
- Reviving woolly mammoths will take more than two years - BBC News [Last Updated On: February 23rd, 2017] [Originally Added On: February 23rd, 2017]
- 20 years after Dolly the sheep's debut, Americans remain skeptical of cloning - Pew Research Center [Last Updated On: February 23rd, 2017] [Originally Added On: February 23rd, 2017]
- Another cloning success shows technology being used by everyday graziers - ABC Online [Last Updated On: February 24th, 2017] [Originally Added On: February 24th, 2017]
- 20 Years After Dolly, Where Are We With Cloning? - Inverse [Last Updated On: February 24th, 2017] [Originally Added On: February 24th, 2017]
- Pabrai And The Shameless Cloning Portfolio - Seeking Alpha [Last Updated On: February 24th, 2017] [Originally Added On: February 24th, 2017]
- As Clone Conspiracy Ends, the Fates of Two Major Spider-Man Villains Are Revealed - Gizmodo [Last Updated On: February 24th, 2017] [Originally Added On: February 24th, 2017]
- 20 years after Dolly the sheep, potential of cloning remains unclear - CNN [Last Updated On: February 24th, 2017] [Originally Added On: February 24th, 2017]
- More lessons from Dolly the sheep: Is a clone really born at age zero ... - Salon [Last Updated On: February 27th, 2017] [Originally Added On: February 27th, 2017]
- 20th Anniversary of Dolly the Cloned Sheep | Men's Health - Men's Health [Last Updated On: February 28th, 2017] [Originally Added On: February 28th, 2017]
- The Angels had two No. 97s on the basepaths, may be cloning their players - MLB.com [Last Updated On: March 1st, 2017] [Originally Added On: March 1st, 2017]
- 20 Years After Dolly: Cloning Past, Present and Future - KQED [Last Updated On: March 2nd, 2017] [Originally Added On: March 2nd, 2017]
- Facts About Cloning - Live Science [Last Updated On: March 2nd, 2017] [Originally Added On: March 2nd, 2017]
- Is Max cloning Tracey the barmaid to take over Walford in ... - Metro - Metro [Last Updated On: March 2nd, 2017] [Originally Added On: March 2nd, 2017]
- We know Dolly the sheep was cloned 20 years ago, but how old was she at birth? - Washington Post [Last Updated On: March 3rd, 2017] [Originally Added On: March 3rd, 2017]
- 'Miracle of nature' Scientists a step closer to HUMAN CLONING after creating mouse embryos - Express.co.uk [Last Updated On: March 3rd, 2017] [Originally Added On: March 3rd, 2017]
- Waxhaw police: Man charged with credit card cloning - WSOC Charlotte [Last Updated On: March 4th, 2017] [Originally Added On: March 4th, 2017]
- Facebook gives zero fucks about cloning Snapchat, adds geostickers in Instagram - TNW [Last Updated On: March 8th, 2017] [Originally Added On: March 8th, 2017]
- This man is cloning old-growth redwoods and planting them in safe places (video) - Treehugger [Last Updated On: March 9th, 2017] [Originally Added On: March 9th, 2017]
- Police warn of criminals cloning credit cards using stolen information - ABC Action News [Last Updated On: March 11th, 2017] [Originally Added On: March 11th, 2017]
- Hard drive imaging vs. cloning: What's the difference? - Windows Central [Last Updated On: March 11th, 2017] [Originally Added On: March 11th, 2017]
- Cloning Your VS 2017 Packages - Microsoft - Channel 9 (blog) [Last Updated On: April 8th, 2017] [Originally Added On: April 8th, 2017]
- Hair Cloning is Happening - NBC 5 Dallas-Fort Worth [Last Updated On: April 8th, 2017] [Originally Added On: April 8th, 2017]
- Woodbury Police Need Your Help In Credit Card Cloning Case - Patch - Patch.com [Last Updated On: April 8th, 2017] [Originally Added On: April 8th, 2017]
- Reasons Against Cloning - VIDEOS & ARTICLES [Last Updated On: April 8th, 2017] [Originally Added On: April 8th, 2017]
- B.o.B Talks Conspiracy Theories About 9/11, Snapchat, Cloning, Chemtrails, The Illuminati & More (VIDEO) - AllHipHop (blog) [Last Updated On: June 6th, 2017] [Originally Added On: June 6th, 2017]
- International grifter gets 5 years in prison for Denver credit card cloning scam - The Denver Post [Last Updated On: June 6th, 2017] [Originally Added On: June 6th, 2017]
- Send in the clones: Orphan Black, TV's smartest show, is back - The Guardian [Last Updated On: June 7th, 2017] [Originally Added On: June 7th, 2017]
- Sorry, 'Jurassic Park' fans: Scientists say dinosaur cloning probably isn't going to happen - Travel+Leisure [Last Updated On: June 7th, 2017] [Originally Added On: June 7th, 2017]
- Preparing winemakers for climate change through cloning - ABC Online [Last Updated On: June 7th, 2017] [Originally Added On: June 7th, 2017]
- Skimming, cloning become popular in Tulsa - KRMG [Last Updated On: June 7th, 2017] [Originally Added On: June 7th, 2017]
- Five Rules For Successful Marijuana Cloning [Last Updated On: June 7th, 2017] [Originally Added On: June 7th, 2017]
- Cloning Grapes Will Save Australian Wine - National Geographic Australia [Last Updated On: June 8th, 2017] [Originally Added On: June 8th, 2017]
- Hackers caught cloning activist Twitter accounts to spread fake news - The Independent [Last Updated On: June 9th, 2017] [Originally Added On: June 9th, 2017]
- Cloning Yourself in Photos or Videos - Fstoppers [Last Updated On: June 11th, 2017] [Originally Added On: June 11th, 2017]
- Cloning to the rescue - New Scientist [Last Updated On: June 14th, 2017] [Originally Added On: June 14th, 2017]
- Cloning To Revive Abaco Wild Horses - Bahamas Tribune [Last Updated On: June 17th, 2017] [Originally Added On: June 17th, 2017]
- How Close Are We to Successfully Cloning the First Human? - Futurism [Last Updated On: June 22nd, 2017] [Originally Added On: June 22nd, 2017]
- Magnified: Cloning - The Hawk Eye (blog) [Last Updated On: June 24th, 2017] [Originally Added On: June 24th, 2017]
- Puppies cloned from ears arrive in Russia for genetic research ... - RT [Last Updated On: June 24th, 2017] [Originally Added On: June 24th, 2017]
- Three waiters arrested in Mumbai for fraud and cloning ATM cards ... - Hindustan Times [Last Updated On: June 26th, 2017] [Originally Added On: June 26th, 2017]