Hey ho,
The release of PHP5 seemed like a good reason to try to learn it once
more...
I'm reading through the O'Reilly PHP/MySQL book and right now, I'm
fiddling with objects. I just discovered the proper way to clone
objects is *not* "$b = $a->__clone();", as it reads in the book, but
"$b = clone $a". Right?
Now it's still not fully clear to me. The class is called UnitCounter,
two private variables are $units and $weightPerUnit. The __clone()
function simply looks like...
$this->weightPerUni t = $that->weightPerUni t;
$this->units = $that->units;
....but when I use it like so:
$a = new UnitCounter();
$a->add(5);
$c = clone $a;
$c->add(5);
....both end up containing 5 units, whereas I would expect $c to contain
5 already from the beginning (as it's a clone of $a, which contains 5
units), so 10 in total, after adding another 5. Why is $c getting the
initial (constructor) value of zero, instead of 5 like $a, which it is
cloned from?
Hoping for someone to enlighten me,
greets,
Tom
The release of PHP5 seemed like a good reason to try to learn it once
more...
I'm reading through the O'Reilly PHP/MySQL book and right now, I'm
fiddling with objects. I just discovered the proper way to clone
objects is *not* "$b = $a->__clone();", as it reads in the book, but
"$b = clone $a". Right?
Now it's still not fully clear to me. The class is called UnitCounter,
two private variables are $units and $weightPerUnit. The __clone()
function simply looks like...
$this->weightPerUni t = $that->weightPerUni t;
$this->units = $that->units;
....but when I use it like so:
$a = new UnitCounter();
$a->add(5);
$c = clone $a;
$c->add(5);
....both end up containing 5 units, whereas I would expect $c to contain
5 already from the beginning (as it's a clone of $a, which contains 5
units), so 10 in total, after adding another 5. Why is $c getting the
initial (constructor) value of zero, instead of 5 like $a, which it is
cloned from?
Hoping for someone to enlighten me,
greets,
Tom
Comment