Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120124 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 87613 invoked from network); 25 Apr 2023 13:46:21 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Apr 2023 13:46:21 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 39B44180511 for ; Tue, 25 Apr 2023 06:46:19 -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,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=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 25 Apr 2023 06:46:18 -0700 (PDT) Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-2fe3fb8e25fso3524731f8f.0 for ; Tue, 25 Apr 2023 06:46:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682430377; x=1685022377; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=sy9BdBZ5J69Hpgw95dH9Dik1Vht8ZQLVdSf/apfzMhw=; b=jykk/ztcNGV4MYEnsdpCDuYRTJMjo+rcwRv8Jz1fuLSJIwSTblRtgwXubC5urEhRlX 0vkKKS4/tWCDJD0x2YlnEW56tkshSaQa8C0qL+SW+puFgm8QNVCp8WgYPvRs669Vocir S5F7bqQgXAQOHyx56VqtpJhWilQ/jRB09uvWXtKFytBjFWFXJoKck0wAoR/IWjnoSpXD 8uZzr6/hWjlbRUILaRt4RFCrkezv3G8zZM+ufOj+nwKjAoxPRBJy08EtG4xGUFCpJVy8 vzo1rKOCIM4XjwOzaqiZPneE/oOhFXkud33E9DI+rlub/oQj31CT8SJJixFRLiJ2jFou hSiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682430377; x=1685022377; 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=sy9BdBZ5J69Hpgw95dH9Dik1Vht8ZQLVdSf/apfzMhw=; b=L5wdlODh1zkC+a5GLhOqDlFE/pOU9naPIYPjp2MPFhXFqAjeuTyyWTQ4jYxIJysJzo 3TjbuB5gi0khFfMJuCINPfBy3+xPHN3BKyfYH2i3GhDeBtb0tDuACcbk0ay07M2aY0dW eHwsepIq0XSzsTfH1TCza06Va6FSBaoOAh3HK5OiTXfHl4RMx/h4x4dZnzlzEtlty2EW MLTObuh1Heau1QT3+/0IY0ongAg5w1006MTdO77Cd7Tl9I3Th2d8ZrDjNpd6vi5u7Nnz HIqqaR2trHV2i9RKHSSOybdswbt9eWYrxkoAXuhjWV+JZaejcal9ICNQ70GuxAqP9Kej agCw== X-Gm-Message-State: AAQBX9eAzFOWaYrHirFpX2HZAi/RkhYPF0PTqcfxH4nr1ww+Yk4RWh4m vOFGKiTRQaYVO8G6fDIkxsAp9IvmuaF1coJndRI= X-Google-Smtp-Source: AKy350ZM14sSoR03djDZ9gEgeg/T3SRX/spgcqqcrlHG01JMQ3p7rY5hIj8Qd5OHNaj1ZQ2loyCek09T2frcQFPNONY= X-Received: by 2002:a05:6000:152:b0:2fb:283a:1751 with SMTP id r18-20020a056000015200b002fb283a1751mr11628956wrx.15.1682430377175; Tue, 25 Apr 2023 06:46:17 -0700 (PDT) MIME-Version: 1.0 References: <67b1e9b0-b7c6-71d4-cad2-2286edab759b@daniil.it> <2F21E8B8-190F-4E46-B42C-5EB490EE2AF7@gmail.com> In-Reply-To: Date: Tue, 25 Apr 2023 15:46:05 +0200 Message-ID: To: Ilija Tovilo Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000fdb08405fa29587f" Subject: Re: [PHP-DEV] Final anonymous classes From: nicolas.grekas+php@gmail.com (Nicolas Grekas) --000000000000fdb08405fa29587f Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi all, > > I've submitted https://github.com/php/php-src/pull/11126 to add support > for final anonymous classes, though as noted by iluuu1994, it would > probably make more sense to just make all anonymous classes final by > default, what do you think? > > > > Extending an anonymous class is indeed possible (https://3v4l.org/pDFTL= ), > but it is a hack as best. If someone wants a non-final class, could they > not write a non-anonymous one? As a bonus, they wouldn=E2=80=99t need to > instantiate the class before referencing it. > > Indeed. The argument was that, if you need to give the anonymous class > a dedicated name through an alias to extend it, you might as well > declare a named class in the first place. > > In case somebody finds benefit in making anonymous classes open, it > seems more sensible to make them opt into openness, rather than > applying this behavior to all anonymous classes that are used as final > 99.9% of the time. Although I really don't think that is necessary. > Fun fact: I wrote test code that extends anonymous classes recently. The typical test case is like this: $obj =3D new class() { public array $foo; }; $proxy =3D $this->createLazyGhost($obj::class, fn () =3D> null); And createLazyGhost() generates a proxy class and returns an instance of it, see https://github.com/symfony/symfony/blob/1f8c5928a1445378eacbaf5b7e1636fdfa8= 610ed/src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php#L415-L= 438 Admittedly this is for convenience in writing test cases, but convenience is what many RFCs are about :) But anyway, there is a related idea I've had in my mind about anonymous classes that I'd like to throw in that conversation: Because they conceptually don't create a new type, I wonder if (final) anonymous classes could be allowed to extend final classes? In order to not allow hacking around and still create a type with class_alias(), we should forbid aliasing final anonymous classes IMHO. Then we could have this discussion about allowing finally anonymous classes to extend final classes. That'd be really useful in many situations where "final" is preventing end users from achieving what they want. Nicolas --000000000000fdb08405fa29587f--