Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124446 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 2E0711A00B7 for ; Tue, 16 Jul 2024 08:14:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1721117741; bh=j8J1YQJwgxxMLAVHBwnIUJMTZm3JX7s/3FeC6xftBj8=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=fYxnaNBWaGw6fjU7tIg/q1bNeINbnH5TNnW3vVolFTXK1GgXjDE6inanND6iwBFol rg7i/4CM/GZSTkfRP+zFAUbcvjfgPI+/MkDWsQGNYv5yVD/xCrqWrhiFQBAlLp0z9U o3SJxRmLJMFWeGMrZnMMcnHWUnbxBSzSZKz7aIVJAuaDFe42oHmCdgsadEInznb9fm rQSpj1TUPZSB3nyjkXoKQV6zzRVlaSFPGXGPfzzAMir/es6tOnlthQ1ckoQYxZIB5f LQqY/FFpWXD7eVnB8qFn7wTXRYQXreXpga4X/PvhunpB2W+NN0xMZ+f4jamwM/+yss 3s5PNdnR43aXg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9D191180F64 for ; Tue, 16 Jul 2024 08:15:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 16 Jul 2024 08:15:40 +0000 (UTC) Received: by mail-lf1-f42.google.com with SMTP id 2adb3069b0e04-52ea8320d48so3996805e87.1 for ; Tue, 16 Jul 2024 01:14:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721117649; x=1721722449; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=j8J1YQJwgxxMLAVHBwnIUJMTZm3JX7s/3FeC6xftBj8=; b=Ykr5phtVBEjWbB/IRNCX+nF1sd+bDwEM+tiM0hwWbbKfvHbCINRemrw0HRwEUk4pUr 4CavqNG9uCcpQDElEfImpOKLDG1ELK4NF9yELLXJo+BRXmSmDPX5IU4gWjhvHaGHaNKG lsgwprR4iOm9oT0eoFkV1S6ClIMqQP078cvs+DGNfBvWQymTJgs6nBA9t9+4Z8rW4Mkb ScDquAM4H3eLV9xce/wWog1cJ5Z7wiq+UzSSiGw8znFv/i+0JGyxcrrxBqCmJIt/ezhC F7uDLGA9ZIve5XgiRUSc9fajk1yuHqhxAsmXF0zcq5hM6lV745zDy/bvQwa4+nP6J/D+ 7GUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721117649; x=1721722449; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=j8J1YQJwgxxMLAVHBwnIUJMTZm3JX7s/3FeC6xftBj8=; b=pKu+04LXf6zbMj0AM48uE53vqwBMw+2GdF0cZb4/Hhy8iPmwmSoTd7qgyR5YZc4TZu L8cj1Pss7cT7j+L83auKmSBR+rIXelN6BKZzUY6SdJnS3vWc97gsg32ySQIf4Q6e2SGc j6SxzgqAbYNRapo13VjG3bz4tHq5jOe2h6ZwuuR3xt1ZrVFScTfYBEmzfM6PBBx4Q2XT 4MTaBqnUMMIYlIC8Tv9Li4gWsrjptqrVYlew5m3KKiHo6BSIIlRkAD4efxYxii5TG/67 Mr4nSuP2wqJyh8Vtk9RXmsovzwWDfjSZJkjnFHvkxrJZcNBRjgoBmRjO24lVP4LTFrbV +nHw== X-Forwarded-Encrypted: i=1; AJvYcCWjCqnR2PItFg5YkRKFvvtCCvNj7Y3v0mEo7Wg2EfA7PjFBYUlrgIAt2rdcaJljzOSVXUIn0cAxLh2EYLLX+sxD/gkE6TND6A== X-Gm-Message-State: AOJu0YwEGDZmhVhBe4DhKaFwFuwFl+HRN0ggqPqsKxo4Ubwp1gMld5KB pLc5K1e5GG+Nue38TndVbsjX9SH/Sze3pewSdjRXoi7pGIgkmIO0bEnDK2L0xfbxUcNI1N00vIf 9TCzkPcARQVyRXzVBUrV5lgU0xQQ= X-Google-Smtp-Source: AGHT+IEGtiUHruM7E9/SovHASVxhgvtbPvfMRzM+Hk+hu8DvssbZelbahyLVvSVeMxb0YOGF5KvB3Zdvnqo/8teyHjw= X-Received: by 2002:a05:6512:3a87:b0:52e:9425:3cc8 with SMTP id 2adb3069b0e04-52edf8c4a07mr329608e87.19.1721117648862; Tue, 16 Jul 2024 01:14:08 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <1118bbcd-a7b4-47bf-bf35-1a36ab4628e1@bastelstu.be> <45847b93-02bf-459f-bcd2-81ba35a12c24@bastelstu.be> <46bd4098-2936-4e46-98e9-fe55118325c2@bastelstu.be> <61ab36bc-b045-452a-84e0-87367d4c680e@bastelstu.be> <07e065f2-8f64-4bad-9a98-51f4eaf63ddb@app.fastmail.com> <2a0a4650-c2c5-4c6d-ad3a-25365b3391b2@bastelstu.be> In-Reply-To: <2a0a4650-c2c5-4c6d-ad3a-25365b3391b2@bastelstu.be> Date: Tue, 16 Jul 2024 10:13:56 +0200 Message-ID: Subject: Re: [PHP-DEV] [RFC] Lazy Objects To: =?UTF-8?Q?Tim_D=C3=BCsterhus?= Cc: Rob Landers , =?UTF-8?Q?Benjamin_Au=C3=9Fenhofer?= , PHP Internals List Content-Type: multipart/alternative; boundary="00000000000013c6e4061d58eeb6" From: nicolas.grekas+php@gmail.com (Nicolas Grekas) --00000000000013c6e4061d58eeb6 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Le lun. 15 juil. 2024 =C3=A0 21:42, Tim D=C3=BCsterhus a= =C3=A9crit : > Hi > > On 7/15/24 09:25, Nicolas Grekas wrote: > > Testing is actually a good domain where resetting lazy objects might op= en > > interesting use cases. > > This reminded me about zenstruck/foundry, which leverages the > > LazyProxyTrait to provide refreshable fixture objects > > < > https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#aut= o-refresh > > > > and provides nice DX thanks to this capability. > > > > I have not used this library before, but I have taken a (very) brief > look at the code and documentation. > > My understanding is that all the fixture objects are generated by a > corresponding Factory class. This factory clearly has the capability of > constructing objects by itself, so it could just create a lazy proxy > instead? > > I'm seeing the `instantiateWith()` example in the documentation where > the user can return a constructed object themselves, but I'm not seeing > how that can safely be combined with the `reset*()` methods: Anything > special the user did to construct the object would be reverted, so the > user might as well rely on the default construction logic of the factory > then. > > What am I missing? > Finding the spot where the reset method would be useful is not easy. Here it is: https://github.com/zenstruck/foundry/blob/v2.0.7/src/Persistence/IsProxy.ph= p#L66-L76 Basically, the reset method is not needed when creating the lazy proxy. But it's needed to refresh it when calling $object->_refresh(). The implementation I just linked swaps the real object bound to the proxy for another one (the line "Configuration::instance()->persistence()->refresh($object);" swaps by reference). --00000000000013c6e4061d58eeb6 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
Le=C2=A0lun. 15 juil. 2024 =C3=A0=C2= =A021:42, Tim D=C3=BCsterhus <tim@ba= stelstu.be> a =C3=A9crit=C2=A0:
Hi

On 7/15/24 09:25, Nicolas Grekas wrote:
> Testing is actually a good domain where resetting lazy objects might o= pen
> interesting use cases.
> This reminded me about zenstruck/foundry, which leverages the
> LazyProxyTrait to provide refreshable fixture objects
> <https://s= ymfony.com/bundles/ZenstruckFoundryBundle/current/index.html#auto-refresh>
> and provides nice DX thanks to this capability.
>

I have not used this library before, but I have taken a (very) brief
look at the code and documentation.

My understanding is that all the fixture objects are generated by a
corresponding Factory class. This factory clearly has the capability of constructing objects by itself, so it could just create a lazy proxy
instead?

I'm seeing the `instantiateWith()` example in the documentation where <= br> the user can return a constructed object themselves, but I'm not seeing=
how that can safely be combined with the `reset*()` methods: Anything
special the user did to construct the object would be reverted, so the
user might as well rely on the default construction logic of the factory then.

What am I missing?


Basically, the res= et method is not needed when creating the lazy proxy. But it's needed t= o refresh it when calling $object->_refresh(). The implementation I just= linked swaps the real object bound to the proxy for another one (the line = "Configuration::instance()->persistence()->refresh($object);&quo= t; swaps by reference).


--00000000000013c6e4061d58eeb6--