Hi Mike,
I will leave others to discuss the use cases. It would be interesting if
people could find the motivating use cases that led to support in C#,
Java, and JS.
I just wanted to comment quickly on these few points, though:
On 13/09/2021 15:05, Mike Schinkel wrote:
1.) Use static analysis to recognize a class has an initialization
functionality and thus provide lint-specific to best practices for static
initialization.
Theoretically, a convention is enough for this, since PHP has no
official static analyser. But certainly a built-in syntax is the
strongest kind of convention, so point taken.
2.) Make the initialization function private.
I'm not sure how that would work: a private constructor means that it
has to be explicitly called from within the same class, but a static
initialiser would be called by the engine, before the class scope even
existed.
3.) Override and/or change order of initialization of parent in child class.
Similarly, I'm not sure this makes sense: it would imply that static
initialisers are inherited, and would be re-run every time a new
sub-class was defined, rather than being run strictly once when the
class is loaded. If the child class wants to re-use code in its static
initialiser, it would make more sense to call a (protected) static
method than to directly repeat the parent's initialiser block.
From your link, it seems that C# restricts them much more than methods
or constructors:
*
A static constructor doesn't take access modifiers or have parameters.
*
A class or struct can only have one static constructor.
*
Static constructors cannot be inherited or overloaded.
*
A static constructor cannot be called directly and is only meant
to be called by the common language runtime (CLR). It is invoked
automatically.
Apparently, the JS implementation does allow multiple static initialiser
blocks in one class, which are simply processed in order. But JS classes
are weird creations, implemented as a complex sugar on top of a
classless object system, so may not be the best point of reference for
PHP, whose object system much more closely resembles Java or C#.
Regards,
--
Rowan Tommins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php