note 72502 modified in language.oop5.cloning by danielc

From: Date: Sun, 27 Jul 2008 05:34:10 +0000
Subject: note 72502 modified in language.oop5.cloning by danielc
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142804@lists.php.net to get a copy of this message
I ran into the same problem of an array of objects inside of an object that I wanted to clone all pointing to the same objects. However, I agreed that serializing the data was not the answer. It was relatively simple, really: 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. --was-- I ran into the same problem of an array of objects inside of an object that I wanted to clone all pointing to the same objects. However, I agreed that serializing the data was not the answer. It was relatively simple, really: 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. http://php.net/manual/en/language.oop5.cloning.php

« previous php.notes (#142804) next »