On Wed, Sep 2, 2020 at 2:11 PM David Rodrigues <david.pro...@gmail.com>
wrote:

> I understand... seems that `$this` is very confusing inside `__clone()`:
> when writing, it writes to the clone, when reading it reads from original.
>
>
That's not an accurate description of what happens today.

$newObj = clone $oldObj;
// 1. Engine creates a new instance of get_class($oldObj), without calling
the constructor
// 2. Engine copies all properties from the old object to the new object
// 3. Engine invokes $newObj->__clone()
  public function __clone() {
    // Userspace object handles any property specific re-initialization
required.
    // $this always refers to the new object here.
 }

The question Niki asked is appropriate; What would one want to do to
$oldObj here?
If the goal is to read from the old object, then you have that already.
The new object is a perfect copy, so read from that.
If the goal is to write to the old object, then justify why you need to do
so, because it's not a clone operation at that point.

-Sara

Reply via email to