Static class initializer in PHP

后端 未结 8 874
南方客
南方客 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:56

    // file Foo.php
    class Foo
    {
      static function init() { /* ... */ }
    }
    
    Foo::init();
    

    This way, the initialization happens when the class file is included. You can make sure this only happens when necessary (and only once) by using autoloading.

提交回复
热议问题