https://github.com/oplanre/php-src/tree/feature/static-class

On Sat, Jun 15, 2024 at 3:17 PM Lanre <lnearw...@gmail.com> wrote:

> 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 AM Bilge <bi...@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/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.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 stage
>> is whether such a PR would have support?
>>
>> Kind regards,
>> Bilge
>>
>

Reply via email to