Prev: CURL cannot connect to URL - IP address - aftersuccessful connection
Next: FPDF passing values into header and footer?
From: Paul M Foster on 26 Apr 2010 00:24 Here is some code: $a = new my_object; $b = $a; My understanding of this operation under PHP 5+ is that $b will now be essentially a "reference" to $a, *not* a *copy* of the $a object. Is this correct? There are cases where I strictly want a *copy* of $a stored in $b. In cases like this, I supply $a's class with a copy() method, and call it like this: $b = $a->copy(); Is this reasonable, or do people have a better/more correct way to do this? Paul -- Paul M. Foster
From: Andrew Ballard on 26 Apr 2010 00:40 On Mon, Apr 26, 2010 at 12:24 AM, Paul M Foster <paulf(a)quillandmouse.com> wrote: > Here is some code: > > $a = new my_object; > $b = $a; > > My understanding of this operation under PHP 5+ is that $b will now be > essentially a "reference" to $a, *not* a *copy* of the $a object. Is > this correct? > > There are cases where I strictly want a *copy* of $a stored in $b. In > cases like this, I supply $a's class with a copy() method, and call it > like this: > > $b = $a->copy(); > > Is this reasonable, or do people have a better/more correct way to do > this? > > Paul > > -- > Paul M. Foster > I've not used it, but isn't that what clone() is for? Andrew
From: richard gray on 26 Apr 2010 00:47
Paul M Foster wrote: > [snip] > > There are cases where I strictly want a *copy* of $a stored in $b. In > cases like this, I supply $a's class with a copy() method, and call it > like this: > > $b = $a->copy(); > > Is this reasonable, or do people have a better/more correct way to do > this? > > Paul > > http://fr.php.net/clone hth rich |