Static class initializer in PHP

后端 未结 8 849
南方客
南方客 2020-12-02 05:12

I have an helper class with some static functions. All the functions in the class require a ‘heavy’ initialization function to run once (as if it were a constructor).

<
8条回答
  •  自闭症患者
    2020-12-02 05:47

    There is a way to call the init() method once and forbid it's usage, you can turn the function into private initializer and ivoke it after class declaration like this:

    class Example {
        private static function init() {
            // do whatever needed for class initialization
        }
    }
    (static function () {
        static::init();
    })->bindTo(null, Example::class)();
    

提交回复
热议问题