Re: Pass source object to clone like __clone($origThis)
| From: | Sara Golemon | Date: | Thu, 03 Sep 2020 14:21:08 +0000 |
| Subject: | Re: Pass source object to clone like __clone($origThis) | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111818@lists.php.net to get a copy of this message | ||
On Wed, Sep 2, 2020 at 2:11 PM David Rodrigues <david.proweb@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