Re: Pass source object to clone like __clone($origThis)
| From: | David Rodrigues | Date: | Thu, 03 Sep 2020 15:39:53 +0000 |
| Subject: | Re: Pass source object to clone like __clone($origThis) | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111825@lists.php.net to get a copy of this message | ||
Now I rethinked about what I said. Really, maybe clone is not the best
option. So maybe we can just use a method that will clone and will have
access to both informations. But I don't know if it solves the original
message.
public function getUserCopy() {
$userCopy = clone $this;
$this->copies[] = $userCopy;
return $userCopy;
}
Considering it, we have access to both now, with "write to source" support
with no additional feature need.
Em qui, 3 de set de 2020 11:21, Sara Golemon <pollita@php.net> escreveu:
> 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
>