Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110945 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 51891 invoked from network); 11 Jul 2020 16:16:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Jul 2020 16:16:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7FA4A180532 for ; Sat, 11 Jul 2020 08:08:34 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.45]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 11 Jul 2020 08:08:34 -0700 (PDT) Received: by mail-ej1-f45.google.com with SMTP id lx13so9303291ejb.4 for ; Sat, 11 Jul 2020 08:08:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=Nsb053SL8XcYSKEi8yn9zSWwL3tljCaOG3sPAU6B4bo=; b=DM5/95rVqmr2ndN7PP4ZS05AhT61xMTl1ICRFzFDsp0j4P75g1qEcuAsHgSS+0LHsG 6KQljyzBXrlWvm3cC9hudwK025zLQbtT7Oom7bJM9+aU6KL188y0wkqgNV8X9QlI1SVu i6x+g634H6VwooZ219Hp9fZb/+69C8hhAAEFrsqnCo5uZ3aqNO0bze4O9cDibVlBbmmA kPoXS0h18P4dqGo0rjmtyQPtRgnHLRYPujuax85p/k5V9diWDzq2k5fYiIN3smrcBgmN wFBfDDj1YmF+sjdZ9kXzCABov+wY5nIuz96ZQbW6xwengsC8cuqiUtm+qebiWoY8AAeW rpQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=Nsb053SL8XcYSKEi8yn9zSWwL3tljCaOG3sPAU6B4bo=; b=hNPpX6bMOswWrOmiPEcUAyXnXbmkKxG/SxvArZj8wQ+8zdyAtF8icElLs4I98imWeH jzxEqAMmjF1OWZ64oof/2Z2EKtt53AhsHWeG9fMKPa4UER6pUOIjLeNDveuKN2ZPrdIG ACnTD3JMeKdQBkhc/be7wVpaZr/P3Yw5Z3lB5kHeaZtihB1VwWYHsOSuRQLf1TyR0HPC Jrpzev9ilK3x3mY/mowShvrKgG673OiFZFnmbfFDWgQqZiVsHo/Qm/rszevvnbHAejFD GVZvg4tyoVG/dYH58V5PwF5iIjbJA3ut30+edECKthFOJC9mC9zIJ8ejVeQAy2o4E5ep eXwg== X-Gm-Message-State: AOAM531MYoD1K9Ev+yG1rz+438OwyHrh7IDtsXAdVlzXUaEw2LqxU7Tx i3AnomOod/oNhp+MF1Q1w0jdv4oGpN2iVI2XXUE= X-Google-Smtp-Source: ABdhPJx/lUvtFPqCXrZoTNlwB1aXxtIjJLPaBeJxDBp3/WDoLRAqOLPWI26+uvBVrC2ZtcmjwG5vqGTUIYoJL4tUnQM= X-Received: by 2002:a17:906:c40d:: with SMTP id u13mr63751148ejz.519.1594480112948; Sat, 11 Jul 2020 08:08:32 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a54:3091:0:0:0:0:0 with HTTP; Sat, 11 Jul 2020 08:08:32 -0700 (PDT) In-Reply-To: References: Date: Sat, 11 Jul 2020 15:08:32 +0000 Message-ID: To: tyson andre Cc: Chuck Adams , PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Possible RFC: UniqueInterface that throws exception at refcount > 1 From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2020-07-11 14:55 GMT, tyson andre : >> Yes, it would be a dynamic uniqueness system, kind of. >> >> Another more flexible alternative would be to add magic methods at >> refcount = 1 and refcount > 1, possibly __owned() and __shared(int >> $refcount), for classes that implement a TrackReferencesInterface or >> similar. But this would need to do ~0 performance hit if not used, >> don't know if possible. > > Then again, php already calls __destruct if it exists when the refcount goes > to 0, > so I may be overestimating the potential performance impact. > > There's a lot of edge cases - probably way more than I mention here. > Running into those edge cases, you might find the feature unsatisfactory if > it were to be implemented. > > For example, > - The UniqueReferenceException itself creates a trace, that increments > reference counts, which might itself cause a UniqueReferenceException. > Workarounds for that would probably involve allowing multiple objects to > reference a UniqueInterface, and then it would be a weaker guarantee. > - unserialize() keeps extra references to ensure that it can delete the > created objects if the original references were overwritten. __wakeup() and > __unserialize() > - The reference count is incremented when calling __destruct(), I think. > > ``` > class UniqueObject implements UniqueInterface{ > public function __construct() { > debug_zval_dump($this); // refcount 3 - and then what about > `$GLOBALS['something'] = $this` inside __construct > } > public function someUtilMethod() { > // object(UniqueObject)#1 (0) refcount(2){ > // } > debug_zval_dump($this); > } > } > // The reference count goes to 2 in someUtilMethod because there's 2 > references. > // the temporary and $this. > (new UniqueObject())->someUtilMethod(); > ``` > > - Tyson All good points. It's better to have a magic method that catches changes in the refcount instead, just like __destruct but on any change, not only at zero. Olle