Skip to content
Advertisement

Static class initializer in PHP

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).

Is there a good practice for achieving this?

The only thing I thought of was calling an init function, and breaking its flow if it has already run once (using a static $initialized var). The problem is that I need to call it on every one of the class’s functions.

Advertisement

Answer

Sounds like you’d be better served by a singleton rather than a bunch of static methods

JavaScript

And then, in usage

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement