Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123622 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 B315E1A009C for ; Sat, 15 Jun 2024 21:17:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1718486335; bh=8CLRa7JaLZcVckS+lbCl4rpEVQDHjhVI1m85wDC0QPY=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Y74stDnOOdICt/rIgHEZTYwziSiwjzW2v65goDt207pRUJ6OvUh+b8sPJHsGfaOmy AvR738ZTltWRzwKv007lHfJPX7sJ5P3MVcw33zD0WCh4cznersClixyN/YYHdKQJkx epk8Jo62V+gzgV+jlo3UF/2emDDhbBPotS6GEt2Up8K0sZ2+kiki8ihCAoFfP0+Fjm pZnpzj67Jydeot8/YjzM2SF7PGuGeO7Xkjafak8QCUH2hMjQuKRFDfYaMimkXD3dSa xjRlo/jreajpe+pyLqDplSb3Jt+cFPQXVgeqaDYj9mMnCqD3HBFOnR9IfGZ154dzCX /zbLMMbsqPluA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1C3FE180069 for ; Sat, 15 Jun 2024 21:18:54 +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-vs1-f54.google.com (mail-vs1-f54.google.com [209.85.217.54]) (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 ; Sat, 15 Jun 2024 21:18:53 +0000 (UTC) Received: by mail-vs1-f54.google.com with SMTP id ada2fe7eead31-48c3957a7c5so1250896137.0 for ; Sat, 15 Jun 2024 14:17:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718486262; x=1719091062; 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=8CLRa7JaLZcVckS+lbCl4rpEVQDHjhVI1m85wDC0QPY=; b=mRIAtMU6/tgQsBTiMGCPdn3hvpcBuhhGJbupIWdd+w3SGKIec2l/wkej7YaXWgWwGC /32RiLcy3f6FGVYNtFwKSIuWhrSSlCfwWT46UMunK8bDBD7eLDTE80O9IAxztAUj2e+l 45lh63KxmGdrPQZZ4PzwGw3Ulk9tpQiEg1j3dp/rRZiRPEUY5vl3iFBzqqLGhZz8TxCh IJ2gKY+aGGslB956bba6ZEglXoz3lKBusNPSTL4pVbHN6eDS0ffXgDeNLtIn+8aAmZJm Z9mp8arVJQ1z/LYhbWGqp4gxmCPY9p/NsBzU7Fgcc4TUAZE4riyOBX/EhtyOK6Rm5sFC UxeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718486262; x=1719091062; 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=8CLRa7JaLZcVckS+lbCl4rpEVQDHjhVI1m85wDC0QPY=; b=XaFOBa0bolfS7FwGZqRPbNbt7v8DI6Uvd+ctgMmgNF4epIUyRDSxMVrleHcDiUCp7l rLIxg4CsNAsWTSEGvzG5vfcbpGXLiGzQSG7y0q9PiQT9S/16ufIm16XqBNzQdpqhs6iV qkT3nFfjOm3rb5vSzPnQe+r3GVMXScVRcehHIOczgqxSB+R3i9OdyS6+cZ/mro7MgR9F 0XmY4qbScfIfkKeycA6bQvpg7IvWVOSdn7U85naZSAXWIToAUdIximCHuX9UYy/P8Ey2 Dqr19zlab8wIkwUUufOpeBQLOMCjVzVPXCuSoTid9AWWsvYoihA4eetsLAVaTWvfHE7t xhGg== X-Gm-Message-State: AOJu0YwJb4IOI1pCUPf8CauvM91Fk2yVfXNvZGZX3Mnv3+N6dEW7cojd AuWXKtaiW20gz4WE6+rT2nzSEMgexstYNBhI7ApZFls+bTse6PG8uICb+EFkM1OGlBb7rf6nmWG pXXUWG5ruzfKSnlytgtYyDoSo/NN6dK8p X-Google-Smtp-Source: AGHT+IHVNL1QjRMK3qEYHWAh0tuWAo2d1gfj/ZrihBIxYFr7U/C3q5XVTOKvsns80gwrd/VwxCitzqAScOYwsSzD2KE= X-Received: by 2002:a05:6102:357a:b0:48c:3c3a:55a4 with SMTP id ada2fe7eead31-48dae3e83c4mr5173177137.35.1718486261936; Sat, 15 Jun 2024 14:17:41 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <0cf69a14-f1b5-4077-9d91-d7b579485eec@scriptfusion.com> In-Reply-To: <0cf69a14-f1b5-4077-9d91-d7b579485eec@scriptfusion.com> Date: Sat, 15 Jun 2024 15:17:31 -0600 Message-ID: Subject: Re: [PHP-DEV] Static class To: Bilge Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="00000000000031bc7a061af443d5" From: lnearwaju@gmail.com (Lanre) --00000000000031bc7a061af443d5 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I proposed this previously and have a working implementation, though it may need some updates. However, I chose not to pursue it further because it was clear it wouldn't pass as many people here are unaware that PHP is a multi-paradigm language. I frequently use static classes because they allow me to have private methods and constants within the class. With namespaced functions, there's no way to declare functions private to the current scope; any function declared in that namespace will be public unless I use a separate internal namespace, which is impractical. I also disagree with the idea that static classes imply global state. They are primarily used for grouping functions. Additionally, there's a general acceptance that we can't autoload functions in PHP, and nothing is being done to address this issue. Yet, it is considered impractical or a "Code Smell" to do one of the only things that make sense. This is a minor change that breaks nothing and would help many people, yet no one is willing to raise any valid arguments against it. On Sat, Jun 15, 2024 at 5:19=E2=80=AFAM Bilge wrot= e: > Hi Internals, > > I want to introduce the `static` keyword at the class declaration level. > That is, the following would be valid: `static class Foo {}`. > > A "static class" is one that only permits static members > (methods/properties) and could have similar semantics to a static class > in C# > < > https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes= -and-structs/static-classes-and-static-class-members>. > > In particular: static classes are implied final and they cannot inherit > from any other class. These specific semantics are negotiable, but in > practice I've never seen the need to mix inheritance with pure static > classes. > > A static class can be emulated somewhat with a trait in userland, and I > have long used such a micro library for this purpose > < > https://github.com/ScriptFUSION/StaticClass/blob/master/src/StaticClass.p= hp>. > > This works by forcing the constructor to be private, thus preventing > instantiation, but it cannot guarantee that the consumer does not create > any instance methods anyway (just that such methods would be useless as > they would be inaccessible). Ergo, it would be better to have this as a > language feature so the compiler can provide the necessary guarantees > and warnings against improper use. > > I am not too familiar with internals, but I figure I can probably muddle > my way through an implementation by following the readonly PR > , which I imagine has > similar touch points. The main thing I'd like to ascertain at this stage > is whether such a PR would have support? > > Kind regards, > Bilge > --00000000000031bc7a061af443d5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I proposed this previously and have a working implementati= on, though it may need some updates. However, I chose not to pursue it furt= her because it was clear it wouldn't pass as many people here are unawa= re that PHP is a multi-paradigm language.

I frequently use static cl= asses because they allow me to have private methods and constants within th= e class. With namespaced functions, there's no way to declare functions= private to the current scope; any function declared in that namespace will= be public unless I use a separate internal namespace, which is impractical= .

I also disagree with the idea that static classes imply global sta= te. They are primarily used for grouping functions.

Additionally, th= ere's a general acceptance that we can't autoload functions in PHP,= and nothing is being done to address this issue. Yet, it is considered imp= ractical or a "Code Smell" to do one of the only things that make= sense.

This is a minor change that breaks nothing and would help ma= ny people, yet no one is willing to raise any valid arguments against it.

On Sat, Jun 15, 2024 at 5:19=E2=80=AFAM Bilge <bilge@scriptfusion.com> wrote:
Hi Internals,

I want to introduce the `static` keyword at the class declaration level. That is, the following would be valid: `static class Foo {}`.

A "static class" is one that only permits static members
(methods/properties) and could have similar semantics to a static class in C#
<https://learn.microsoft.com/en-us/dotnet/cshar= p/programming-guide/classes-and-structs/static-classes-and-static-class-mem= bers>.
In particular: static classes are implied final and they cannot inherit from any other class. These specific semantics are negotiable, but in
practice I've never seen the need to mix inheritance with pure static <= br> classes.

A static class can be emulated somewhat with a trait in userland, and I have long used such a micro library for this purpose
<https://github.com/Sc= riptFUSION/StaticClass/blob/master/src/StaticClass.php>.
This works by forcing the constructor to be private, thus preventing
instantiation, but it cannot guarantee that the consumer does not create any instance methods anyway (just that such methods would be useless as they would be inaccessible). Ergo, it would be better to have this as a language feature so the compiler can provide the necessary guarantees
and warnings against improper use.

I am not too familiar with internals, but I figure I can probably muddle my way through an implementation by following the readonly PR
<https://github.com/php/php-src/pull/7305/files= >, which I imagine has
similar touch points. The main thing I'd like to ascertain at this stag= e
is whether such a PR would have support?

Kind regards,
Bilge
--00000000000031bc7a061af443d5--