Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123793 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 5E2C81A009C for ; Mon, 24 Jun 2024 21:56:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1719266253; bh=oOZVbF/pkRcr7kKrPQftSbuQSkwE+IOWLEf5iYt5k04=; h=References:In-Reply-To:Reply-To:From:Date:Subject:To:Cc:From; b=dUqZTV+ZVwbqR1oEQXHwuvHp5B0Mbedxqm1KyE/XujPSUSFIbkeTFmiq5yKkTh5FM QVdlb2MrK/ncj97BGJDjEjy35H+Y3mSwN718Od+oXVVySOQffC0DfgX8DYarZuFPg7 n18nzGunyuW/EhZLMWMn9fG2eVMGdWv+ym6kvmWiHV8yNpFGLWthLcQuVh4VH5L6xl N7DR4rylNKnMJYeypR99zVq8xG6PKKihQF6CypqxBzoGx/kAOeSMSOV6rMqKyiTPGv FpahCdhQZGQVHTVMHxHQka0UgPWoDWITWUAhsw/4NJbV1JSaNLcTt3BS/tKBsIFXd4 g4ioyrpYRD82g== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C5C221804D3 for ; Mon, 24 Jun 2024 21:57:32 +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=1.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,FREEMAIL_REPLYTO_END_DIGIT, 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-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) (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 ; Mon, 24 Jun 2024 21:57:29 +0000 (UTC) Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-643603b1feaso20786987b3.0 for ; Mon, 24 Jun 2024 14:56:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719266172; x=1719870972; darn=lists.php.net; h=cc:to:subject:message-id:date:from:reply-to:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=9OjWdWzO1P1VHsMForqeuBg5ixdfhfKrgSxVbTsaMio=; b=cL/mEw5fCdnfSC20UF+JYPLdnz2e4edpDsI5eF5chYbHhiyEZPgJW2+FF7sTz3X6O9 JOW0+HYnV9TKNXLMCziPxMVWUx3TQN8LYP7W79vKeiTLrPwa4yjxdKeqrsYu0PcROzCw IzZZPZ0NrH3+Gcqk0o7U/R2FtOvDIjiEe/V/e99CFsTDrk/BtLIGrt33ipEJy0ej/+bl 2Q80BhnkBH23S0zb+FeCL4v0woNFqAeATkn3/y4B6HAIud/GyI/7FHPVbsWnInoRjiP+ DJr+ZAgGmcxbrYmHLZj1x3jipaPbUnm3P9q2UMb4vIbN2pNKer+/DThMvFM5uJ86CJ/T VOMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719266172; x=1719870972; h=cc:to:subject:message-id:date:from:reply-to:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9OjWdWzO1P1VHsMForqeuBg5ixdfhfKrgSxVbTsaMio=; b=dKxsASMQLNMF6LGoog601lw5YZapzDpzcX+MXhHxwP0Z4pxocEiaedUKIjae/p8IHT 3BpL+ZTxcBRQQlxkIX7CTQgmdlVIACL0/Mj7yMZL5USzYMlbHU6vgR/Q6SIwaTRBFAAj MKl2Pii9Yv1Nf2z3l03KOlx1kVVTTkEh86CjTD1yNX1iR3BM7704pfBvHF8GbZSpQpK5 w59EQUCx+wVyVkdIL+6T/V96fv5u+uMvTeh3DGqBp042L0Ae+fGL2wUg5+7WeXadwdvM Agvo7PpDkSrM40BYtKB7kfw51ginQ2IiXJD4voxon7qczI4bg3EJKNzRXiRClDl2EsIi AwsA== X-Gm-Message-State: AOJu0YwdW1gva+bAo8l6ysQllPMFW2Pl2fKlx/Koocy7IUezqJz8CeTW nm0SmO7A8Gyc7BV9TQCCB8gf8mXjOqJ08mnKT5mwgSka69+MDimfeXALMjyTaBGLyobTJT21B3t Cj1LKcKS76IOubO+9IE65RhG4bdCFZjLa X-Google-Smtp-Source: AGHT+IGRDJ9iTJ1T5EISt2XWgNJSIEFwi/aWlPcHHDIyHYlnZ6SY6etatt8xrGdXBqppDHTFgLc/RFMpEw5rmp2j3N4= X-Received: by 2002:a05:6902:102f:b0:e02:b7ee:5354 with SMTP id 3f1490d57ef6-e0300f893d9mr7182521276.20.1719266171861; Mon, 24 Jun 2024 14:56:11 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: In-Reply-To: Reply-To: autaut03@gmail.com Date: Tue, 25 Jun 2024 00:56:00 +0300 Message-ID: Subject: Re: [PHP-DEV] [RFC] Static class To: Bilge Cc: php internals Content-Type: multipart/alternative; boundary="00000000000072c168061ba9d925" From: autaut03@gmail.com (Alex Wells) --00000000000072c168061ba9d925 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hey Bilge, I'm not usually a resident of these discussions, but it seems like this RFC is heading into a wrong direction. Let me explain: as it currently stands, static properties and methods live separately from instance properties and methods. That means a couple of limitations, namely: a static class member can never implement an interface's contract, and hence a static class could never be used as an instance; static class members have a separate reflection API; static class members cannot have expression initializers; there's no static constructor; and so on. Adding `static classes` would not solve any of the above issues, and they would still be barely useful. To counter these issues, Kotlin has a concept of `data objects`: < https://kotlinlang.org/docs/object-declarations.html#data-objects>. They look like regular classes, but only one instance of a data object is created - when it's first accessed by other code. The closest alternative in PHP would be an enum with a single case. Unlike static classes, `data objects` don't have any of the problems associated with static members. They can be passed around as an instance of a class. This allows implementing interfaces, and replacing a `data object` with a regular class instance without having to change all member access from static to instance. They would also be able to inherit the syntax and semantics of regular class constructors: ``` object Converter { private array $map =3D new Map(); public function __construct() { $this->map['miles to km'] =3D 1.6; } public function convert(string $type, float $value): float { return $value * $this->map[$type]; } } doSomeConversion(Converter::object); // If `Converter` is ever to become a regular class, none of the related code would require changes. function doSomeConversion(Converter $converter) { $result =3D $someUtils->convert('miles to km', 123); } ``` That would be functionally equivalent to: ``` class Converter { private static self $instance; public static function instance(): self { return self::$instance ??=3D new self(); } public function convert(float $something): float {} } doSomeConversion(Converter::instance()); ``` Of course, this is already possible, as shown in the second example. But so is this RFC. Implementing data objects would actually bring them on-par with regular classes, and likely cause less drama overall. On Mon, Jun 24, 2024 at 2:14=E2=80=AFAM Bilge wrot= e: > Hi Internals! > > I am pleased to present my first RFC: Static class > . > > This work is based on the previous discussion thread on this list of the > same name, and hopefully captured all the relevant details, > notwithstanding anything unanticipated that may present itself during > the implementation. Let me know if you feel anything important has been > missed. I am aware it omits to mention specifics about messages so > emitted when runtime or compile-time errors occur, but I anticipate this > can be hashed out in the PR, unless consensus indicates otherwise. > > I am aware this idea is not supported by everyone, but there seemed to > be enough positive voices for it to be worth a shot. I'd like to get a > better idea of where people might stand when it comes down to a vote, > now there is a formal RFC, so we know whether it's worth completing the > implementation, although any sentiments so proffered are of course not a > commitment to vote any particular way and nobody should feel compelled > to speak to that unless comfortable. Looking forward to feedback! > > Cheers, > Bilge > --00000000000072c168061ba9d925 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hey Bilge,

I'm not usually a reside= nt of these discussions, but it seems like this RFC is heading into a wrong= direction. Let me explain: as it currently stands, static properties and m= ethods live separately from instance properties and methods. That means a c= ouple of limitations, namely: a static class member can never implement an = interface's contract, and hence a static class could never be used as a= n instance; static class members have a separate reflection API; static cla= ss members cannot have expression initializers; there's no static const= ructor; and so on. Adding `static classes` would not solve any of the above= issues, and they would still be barely useful.

To= counter these issues, Kotlin has a concept of `data objects`: <http= s://kotlinlang.org/docs/object-declarations.html#data-objects>. They= look like regular classes, but only one instance of a data object is creat= ed - when it's first accessed by other code. The closest alternative in= PHP would be an enum with a single case. Unlike static classes, `data obje= cts` don't have any of the problems associated with static members.=C2= =A0

They can be=C2=A0passed around as an instance = of a class. This allows implementing interfaces, and replacing a `data obje= ct` with a regular class instance without having to change all member acces= s from static to instance. They would also be able to inherit the syntax an= d semantics of regular class constructors:

```
object Converter {
=C2=A0 =C2=A0 private array $map =3D ne= w Map();

=C2=A0 =C2=A0 public function __construct= () {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $this->map['miles to km'] = =3D 1.6;
=C2=A0 =C2=A0 }

=C2=A0 =C2=A0=C2=A0=C2=A0public f= unction convert(string $type, float $value): float {
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 return $value * $this->map[$type];
=C2=A0 =C2=A0 }
}

doSomeConversion(Converter::object);
<= br>
// If `Converter` is ever to become a regular class, none of = the related code would require changes.
function doSomeConversion= (Converter=C2=A0$converter) {
=C2=A0 =C2=A0 $result=C2=A0 =3D $so= meUtils->convert('miles to km', 123);
}
```

That would be functionally equivalent to:

<= /div>
```
class Converter {
=C2=A0 =C2=A0 private s= tatic self $instance;

=C2=A0 =C2=A0 public static = function instance(): self {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return se= lf::$instance ??=3D new self();
=C2=A0 =C2=A0 }

=C2=A0 =C2= =A0 public function convert(float $something): float {}
}

doSomeConversion(Converter::instance());
``= `

Of course, this is already possible, as shown in= the second example. But so is this RFC. Implementing data objects would ac= tually bring them on-par with regular classes, and likely cause less drama = overall.

On Mon, Jun 24, 2024 at 2:14=E2=80=AFAM Bilge <bilge@scriptfusion.com> wrote:
Hi Internals!

I am pleased to present my first RFC: Static class
<https://wiki.php.net/rfc/static_class>.

This work is based on the previous discussion thread on this list of the same name, and hopefully captured all the relevant details,
notwithstanding anything unanticipated that may present itself during
the implementation. Let me know if you feel anything important has been missed. I am aware it omits to mention specifics about messages so
emitted when runtime or compile-time errors occur, but I anticipate this can be hashed out in the PR, unless consensus indicates otherwise.

I am aware this idea is not supported by everyone, but there seemed to
be enough positive voices for it to be worth a shot. I'd like to get a =
better idea of where people might stand when it comes down to a vote,
now there is a formal RFC, so we know whether it's worth completing the=
implementation, although any sentiments so proffered are of course not a commitment to vote any particular way and nobody should feel compelled
to speak to that unless comfortable. Looking forward to feedback!

Cheers,
Bilge
--00000000000072c168061ba9d925--