Static constructors
Posted by HorrorkidIP 5 years ago
Currently you can't initialize static variables with code or methods.
That's why a static constructor would be really helpful.
When accessing a static variable the static variables can be initialized within the static constructor. similar like this:
``` php
class StaticHelperClass
{
public static $usefulVariable;
public static $mediocreUsefulVariable = "secret cookie";
#static constructor
static function __construct()
{
self::$usefulVariable = self::configureVariable();
}
private static function configureVariable()
{
#do interesting stuff
}
}
```
I have C#'s static constructors in my mind and the best and shortest explanation I found would be the following one:
"A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced."
I bet they would make a great addition to PHP.