Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111832 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 33889 invoked from network); 3 Sep 2020 17:31:44 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Sep 2020 17:31:44 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 082CD180543 for ; Thu, 3 Sep 2020 09:36:56 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,HTML_MESSAGE, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-mahalux.mvorisek.com (mail-mahalux.mvorisek.com [77.93.195.127]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 3 Sep 2020 09:36:55 -0700 (PDT) Received: from c20eb07340d2 (10.228.0.234) by mail-mahalux.mvorisek.com (10.228.0.4) with Microsoft SMTP Server (TLS); Thu, 3 Sep 2020 18:36:47 +0200 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_201f03fa0440946742b9098a4517c3c8" Date: Thu, 03 Sep 2020 18:36:46 +0200 To: Sara Golemon Cc: PHP internals In-Reply-To: References: <2868e0ed4e7ebc09b568471c913079516f93fd2fc95e97b0ffea192db1e04275@mahalux.com> Message-ID: <338f74ebb60ebac19ec4316f2e64d720988c60df9ac9e116a5ab364974fea8fe@mahalux.com> X-Mailer: SAP NetWeaver 7.03 Subject: Re: [PHP-DEV] Pass source object to clone like __clone($origThis) From: vorismi3@fel.cvut.cz (=?UTF-8?Q?Michael_Vo=C5=99=C3=AD=C5=A1ek_-_=C4=8CVUT_FEL?=) --=_201f03fa0440946742b9098a4517c3c8 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8; format=flowed The goal is to be able to access the original object and it's id/hash. Usecases: - something is associated with the object using the object id/hash and it needs to be cloned as well we need the original object to obtain it's id/hash for spl_object_id and spl_object_hash methods - bounded Closures ar associated with the original object and they needs to be rebound to the cloned object example: public function __clone(self $origThis) { if ((new \ReflectionFunction($this->fx))->getClosureThis() === $origThis) { $this->fx = \Closure::bind($this->fx, $this); } } Modification of php is simple: https://github.com/php/php-src/pull/6063/files#diff-beea8c5a8ceb318220b34b73e4ecfc98R252 we simply pass the old object as 1 parameter. I belive, passing old object have positives and no performance nor compatibility impact. All other current solutions require an extra property and a lot of code, as assigning in constructor is not enough (due serialization etc.), or it is even impossible, if object was created using reflection without constructor. With kind regards / Mit freundlichen Grüßen / S přátelským pozdravem, Michael Voříšek On 3 Sep 2020 18:00, Sara Golemon wrote: > On Thu, Sep 3, 2020 at 10:40 AM David Rodrigues > wrote: > >> 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; >> } > If your goal is to track copies, then a static makes much more sense. > > class AllKnowing { > private static $copies = []; > > public function __construct(...) { > self::$copies[] = $this; > .... > } > > public function __clone() { > self::$copies[] = $this; > } > } > > -Sara --=_201f03fa0440946742b9098a4517c3c8--