Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114169 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 64851 invoked from network); 25 Apr 2021 21:52:50 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Apr 2021 21:52:50 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C8F8D1804DC for ; Sun, 25 Apr 2021 14:56:42 -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 autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-vs1-f41.google.com (mail-vs1-f41.google.com [209.85.217.41]) (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 ; Sun, 25 Apr 2021 14:56:39 -0700 (PDT) Received: by mail-vs1-f41.google.com with SMTP id h18so686388vsp.8 for ; Sun, 25 Apr 2021 14:56:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=xPg8v9/QLuCd3f0aoejDoHS2a69lbeTGuPka6CKJxE0=; b=Mq7fK6uiMtevDkb4XCGzImhmQVCG7NoXIC7op/0JiYMRBttpZ1Q0EJ9s1Ett7qrFYk r6bozp91v6hdMsfbTkscudNvUdBYdTzfv6kBjhHjFAT1XZSrfRAGz3F/l/uyrk4BZ6y2 ZGlZyVODBHcqKXx/+r7gBQwRkSiTOWHFUtVWB/Oha50wRdD//TveiivFoaC3/xqoYyBR Z4QOit3C+/Lr5MzGm3kah/ZUiWFd/lU8EclI4vdXt2ANVlTyYyW1sYnsLRnJvx4ghSE2 ITZ/kqoqny1Kp2kvfnffpWiHdLGpmMZ6X3rgxEFvvmM3D1WedymGu9ne5eQ7uiFZmcwB sFdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=xPg8v9/QLuCd3f0aoejDoHS2a69lbeTGuPka6CKJxE0=; b=rZRZkEFfC5YLC9zWmPqHaFtxSAUFoo9Gf+bRn43TA4BuqaE7hfQxsrKTHIWiHbPzmY oYvZ93F2P2wAkvkeDW4U5qn7aryWOaryMvkQyV7CH1KgMHqwpxluBwkXo6o3CQeXKoJa usgjQCHcmX7rEI19/5PJNO91mRvKVV5DtanYAZayx6ybOCvDcZDKfjhfMIY8vL9qfJup PNx5o2aA7YUoz+i4o3fHtuT51E9iUg6TUBR2iU/mhWxIqaQBqpVndtDnDgt5+3xB9+1U 1O+6JSzdzn1mp9b6xBso3/YSLoxZQI3SHABLh3vkyZybao2sqm5EraclvjUg7e+qePRW ywsg== X-Gm-Message-State: AOAM533R128Uj4DySoBAQCkjfn09glUPfRdq+BaTE+OjWMs8sM7DW/HQ f2YBDqv+2ROJkfxX80YcYHezCz6B0gKjWc0hDxQ= X-Google-Smtp-Source: ABdhPJwBKFFqc4SaSiimBcGCR8b0qVlENcoGRWxLoQxXCsJVgE47mWKxmXn+ivj76was+ZvVLs4QyyCtgs75X0j8yT8= X-Received: by 2002:a67:c48a:: with SMTP id d10mr1577064vsk.13.1619387795198; Sun, 25 Apr 2021 14:56:35 -0700 (PDT) MIME-Version: 1.0 References: <5b9f1500-615a-48f1-815f-1d48b327ef90@processus.org> <179049b1475.11134368b213512.254739612773841999@void.tn> <0BF84585-DDC3-4B25-BFD2-D8B916D135EE@newclarity.net> In-Reply-To: <0BF84585-DDC3-4B25-BFD2-D8B916D135EE@newclarity.net> Date: Sun, 25 Apr 2021 17:56:26 -0400 Message-ID: To: Mike Schinkel Cc: David Gebler , PHP Internals Content-Type: multipart/alternative; boundary="00000000000049560105c0d31a1b" Subject: Re: [PHP-DEV] [RFC][Draft] Sealed Classes From: chasepeeler@gmail.com (Chase Peeler) --00000000000049560105c0d31a1b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, Apr 25, 2021 at 11:36 AM Mike Schinkel wrote: > > > > On Apr 24, 2021, at 7:39 PM, David Gebler wrote= : > > > > I don't love this idea, I'm not very fond of the final keyword, either; > > I'll start by saying the final keyword caused me a tremendous amount of > heartache because it was used on a class in a framework that I badly, bad= ly > needed to extend. > > But even so, I recognize why they used it, and I still don't have a great > argument for how they could address the reasons they used it some other w= ay. > > > I've always believed annotations (or attributes in PHP these days) are = a > > better of way of indicating you, as an author of a class, did not write > it > > with inheritability in mind or intended than restricting language > features > > through syntactic constructs. > > > > The RFC says "when you have a class in your code base that shares some > > implementation detail between 2 or more other objects, your only > protection > > against others making use of this class is to add `@internal` annotatio= n, > > which doesn't offer any runtime guarantee that no one is extending this > > object", to which I ask - why do you need this guarantee? What does it > > qualitatively add? If I make a judgement that I want to extend your cla= ss > > or implement your interface, I can just delete the sealed keyword from > your > > code and carry on. So it doesn't actually offer any guarantee at all th= at > > I'm not extending the type. > > Actually, it does offer such a guarantee. It guarantees if you are using > a non-forked version of the original developer's (OD's) library or > framework then that class won't be extended. When someone pulls the > original non-forked version from its source repository =E2=80=94 such as = when using > Composer =E2=80=94 then that code will be (effectively) guaranteed not to= be > extended. > > OTOH, if you do delete the sealed (or final) keyword you have then forked > the code, in a defacto manner if not a literal one. If you use a forked > version of the code, you now own the maintenance of that code and any bug= s > that are generated by your forked changes in using code. The original > developer has no moral, ethical or even contractual obligation to care > about the breakage you cause. > I'd argue that if the original developer made clear that you shouldn't extend a class, then they still have no moral, ethical, or even contractual obligation to care about the fact you've used the object in a way they were clear was not supported. I'm with David on this one. I can understand the need to enforce a final/sealed concept for core functionality implemented in C which might do some funny things under the hood. I don't think that should be extended to userland. If you want to warn someone, that's fine. But don't totally prohibit them. Given the ability for composer to pull from forked repos and the easy of keeping a forked repo in sync with it's upstream version, creating a fork just to remove a sealed/final designation isn't that difficult to do. > > Hypothetical example: You fork the code, remove sealed/final, then > subclass the code and add a method, let's call it ToString(). And you wri= te > your application to use ToString(). Now the OD releases a new minor versi= on > and they also add a ToString() method. Applications using your fork > probably cannot use the new version of the OD's library because when the > library calls ToString() your version is called. So you have to update yo= ur > application to use the new version of the library and once again remove > sealed/final. > > AND, if your code is instead another add-on library, now users of your > add-on library will also have to fix their code too. Which could > potentially be a large number of users if your add-on is successful. > > So not using final or sealed can result in some really hairy and possibly > impossible to fully resolve backward compatibility concerns for developer= s > who publish libraries and/or frameworks. > > > The best it can achieve is to indicate your > > intentions, which I believe can be adequately done today through an > > attribute, no addition to the language needed. > > Still, I concur with your concerns. Developers too often implement final > classes in libraries and frameworks without fully addressing all the > use-cases and/or adding enough extensibility points because it makes thei= r > lives easier. Because of that final =E2=80=94 and sealed, if added =E2= =80=94 can make the > life of an application developer a living hell. > > So what's the answer? I don't know that I have the ultimate answer, but = I > would be a lot more comfortable with adding features to PHP such as ones > like sealed that restrict the "O" in S.O.L.I.D.[0] if PHP were to offer t= he > following three (3) things, all of which can be found in Go, and I am sur= e > other languages: > > 1. Class embedding[1] =E2=80=94 Allows one class to embed another and imm= ediately > have access to all its properties and methods, and also to be able to > extract an instance of that embedded class. It is called "Type embedding= " > in Go. > > 2.Type definitions[2] =E2=80=94 A typedef would allow developers to defin= e > constrained versions of existing types, such as `FiveStarRating` which > could only contain 1, 2, 3, 4 or 5, or types that identify a signature, f= or > example as `ConvertToString` which could require a closure that implement= s > `func(mixed):string`. In Go you can compose other types to create new > types, but I'm not sure if those other type of types could apply to PHP, = at > least as it currently exists, and especially because it is not a compiled > language. > > 3. Structural typing[3] =E2=80=94 Basically interfaces that can be implem= ented > implicitly rather than explicitly. For example, if I wanted to implement= a > Stringable interface that requires a ToString():string method then > structural typing would allow me to implement that interface simply by > adding a ToString() method instead of requiring me to also add "implement= s > Stringable" to the class definition. > > Those three features are all killer language features and would make grea= t > additions to PHP. IMO, of course. > > #fwiw > > -Mike > > [0] https://stackify.com/solid-design-open-closed-principle/ > [1] https://travix.io/type-embedding-in-go-ba40dd4264df > [2] https://go101.org/article/type-system-overview.html > [3] > https://blog.carbonfive.com/structural-typing-compile-time-duck-typing/ < > https://blog.carbonfive.com/structural-typing-compile-time-duck-typing/> --=20 Chase Peeler chasepeeler@gmail.com --00000000000049560105c0d31a1b--