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