Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123529 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 BA5B11A009C for ; Thu, 6 Jun 2024 06:53:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1717656865; bh=tJBjdCVlsxe8HhE/3YrS5Q3mWDXt0U6dhKVxCnivSMA=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=eJxFe4/zvYqIkHAItLu6/R3ddKugh6H2FJXEmq2mZKcjhs6TSrR1VsJaIndB9vS6f aUw3+10bSzSeoZAMQLYji/o9E1gmaBMlockOYD2dHMuxTYN4CWOGSlYvIa3E3S13Wk Rk6WYEQkYm1rKQPf8UwIQwjkUAriPr5NAlzIbR3xWX0utvQ71+iZqWlJvHv4K33xEI xWRGqn1JBDfu/ken2JwQii2hBTptMJnfnHKL+hW5Iqkkt7rX79pJXw9baYNj5C9JtW eYT1TycSX7YCmbLU18VzBMwOvw8VQnary/VQXc9BxaWkCfuNxR2npGjql4eQrOjpPw YWQ6E1E4T9i8Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5F257180068 for ; Thu, 6 Jun 2024 06:54:24 +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,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) (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 ; Thu, 6 Jun 2024 06:54:21 +0000 (UTC) Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-627efad69b4so5594437b3.3 for ; Wed, 05 Jun 2024 23:53:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717656795; x=1718261595; 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=+0Ijply/OWdEwk8K5QcJUId2z0e6ldNyqPqH5WXl9Vg=; b=nWkaEnuKR6bP+w0CJh5aFIe8/yy64fD612Mq3ogEEM2ddYOB1Q30oF71oXB8Iyzhfn HRJNni43r3Yp8+5HPkA3qExztN2kcWrakz92K5fTE+I+uo6tS+X6u8anFAE9AYWQYRAU 7KXUKUIT/g6gcpLYz9gVtU8Wf9vL9ch1MXu7o3wuw2BXm2D+UbjkCTas/JhSiBjqP4xu RSy6WueEtWlF38YlCSpJ5s+7rbsH5lRJLyJGYxOk805jwoqZ+T3vhyAelLFfAA8uDO4J un6/3J1cq18vqWqzR/5tctA/HOP9NzsGaxLpvLD20ZzRFdlNh8e5VVhDPOdETq93ht1x 11Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717656795; x=1718261595; 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=+0Ijply/OWdEwk8K5QcJUId2z0e6ldNyqPqH5WXl9Vg=; b=gd5Qemz7JTay9ShjZ2fGCWI4vmoFMi+Rz9Gro2JK0oFnrrE84KEgVOpZfSDBfknUJe WZKZPqBPruBPVpsTneeJxSIDMaUdU7by1nFcpXLbbOqUS8wkC8D/2JqZETsZp9xJntWO s036/lLsLkJqT34g2EXAlnfE1yK0lcLUOnUI/luMowjF4Fs/KhqUs0xYN6W1sqR8fKY3 bkHlxcXf9iBr0Gv6OgM353E2zes5ciTnMJTXq2zJcwPugzF0Q36ERuiDHd4CxCYnABjC JBUyXkqkBcNMymdHBOwqbTodu5FS+nuSZ3SykvL9/mGXtOHmeHn4KY0bIFOROsU1wcFt DQNw== X-Forwarded-Encrypted: i=1; AJvYcCUSvP6WNsA6hK146NTdbEqCtuBNcKrkH2oHUTyYQR0C7vO9Qyi9qHTZpigAwRvBHveya23nVd0aLNpDFjbVyaQX0j1kIYdmvw== X-Gm-Message-State: AOJu0YxxeebhKpZipklzZzeYj/b1W9XqXpbhRGjv0b4pKMATzFkcXMhl B08J1QGbHJmZhgwLbpq4T1f5VgWkC64+O3EUh2uJuvdXgCa7lZeZvW0IaxXqwoqlM4gu2/A3bWG OZGGlSPg+7+rFfJtHFnG1n1pKmeU= X-Google-Smtp-Source: AGHT+IEj1oZRLJcnu9PvX3E410zPcYOKzELZuOZ6h8QJ3PPXelvCReB9ajd7Xsl+oYCt3mQVU+2YTCT3ff/RvjScDnY= X-Received: by 2002:a0d:cc06:0:b0:62c:c641:f95a with SMTP id 00721157ae682-62cc64284e4mr21851467b3.42.1717656795124; Wed, 05 Jun 2024 23:53:15 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 6 Jun 2024 08:53:04 +0200 Message-ID: Subject: Re: [PHP-DEV] [RFC] Lazy Objects To: Arnaud Le Blanc Cc: Larry Garfield , php internals Content-Type: multipart/alternative; boundary="0000000000001e9d4c061a3323ec" From: michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Marcin_Brzuchalski?=) --0000000000001e9d4c061a3323ec Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Arnaud, =C5=9Br., 5 cze 2024 o 20:08 Arnaud Le Blanc napisa= =C5=82(a): > Hi Larry, > > Thank you for the feedback. > > I think you got the two strategies right. However, there is a use-case > in which an object manages its own laziness by making itself lazy: > > ``` > class C { > public function __construct() { > ReflectionLazyObject::makeLazyGhost($this, $this->init(...)); > } > } > ``` > > This one can not be addressed by a newInstance*() method since the > object to be made lazy already exists. > Did you consider implementing it using some attribute? On constructor like: ``` class C { #[LazyInitialization] public function __construct(private readonly string $foo) { // ... init executes after first use, but all promoted properties are already initialized } } ``` or on specialized initializer: ``` class C { public function __construct(private readonly string $foo) { // do something light } #[LazyInitialization] private function initi(): void { // do something heavy } } ``` I don't know if this is a good example of doing the same thing or if it doesn't limit functionality, but for me, it is way more clean and easier to understand. Cheers, Micha=C5=82 Marcin Brzuchalski --0000000000001e9d4c061a3323ec Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi Arnaud,

=C5=9Br., 5 cze 2024 o 20:08=C2= =A0Arnaud Le Blanc <arnaud.lb@gma= il.com> napisa=C5=82(a):
Hi Larry,

Thank you for the feedback.

I think you got the two strategies right. However, there is a use-case
in which an object manages its own laziness by making itself lazy:

```
class C {
=C2=A0 =C2=A0 =C2=A0public function __construct() {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 ReflectionLazyObject::makeLazyGhost($this, $thi= s->init(...));
=C2=A0 =C2=A0 }
}
```

This one can not be addressed by a newInstance*() method since the
object to be made lazy already exists.
=C2=A0
Did you consider implementing it using some attribute?

On constructor like:
```
class C {
=C2=A0 =C2= =A0 #[LazyInitialization]
=C2=A0 =C2=A0 =C2=A0public function __construc= t(private readonly string $foo) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 // = ... init executes after first use, but all promoted properties are already = initialized
=C2=A0 =C2=A0 }
}
```

or = on specialized initializer:

```
class C {
= =C2=A0 =C2=A0 =C2=A0public function __construct(private readonly string $fo= o) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 // do something light
=C2=A0 =C2=A0 = }
=C2=A0 =C2=A0=C2=A0
=C2=A0 =C2=A0 #[LazyInitializatio= n]
=C2=A0 =C2=A0 private function initi(): void
=C2=A0 = =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 // do something heavy
<= div>=C2=A0 =C2=A0 }
}
```

I don't kn= ow if this is a good example of doing the same thing or if it doesn't l= imit functionality,
but for me, it is way more clean and easier t= o understand.

Cheers,
Micha=C5=82 Marcin= Brzuchalski
--0000000000001e9d4c061a3323ec--