Are there any better solutions or is my design template completely wrong in this case?
Destructors are called only for objects, and not for static classes.
Instead, you can convert your class from static to regular to instantiate it. Then he will have a destructor. In addition, this code simplifies reuse and testing.
In addition, you can implement magic methods for __get and __set or ArrayAccess , which are often useful for simple storage and access to data, as for configuration.
Alternatively, you can add a destructor object to a member of the static class to achieve what you are looking for:
class ConfigDestructor { public function __destruct() { Config::destruct(); } } class Config { static private $destructorInstance; static private $autoSave; static public function get() {} static public function init($autoSave) { if (null === self::$destructorInstance) self::$destructorInstance = new ConfigDestructor(); self::$autoSave = $autoSave; } static public function destruct() { if (self::$autoSave) self::save(); } }
Just FYI: You wrote that you want to add an automatic save function. There is a common gap for __destruct() both __destruct() and register_shutdown_function :
Note. The script working directory may change inside the break function on some web servers, for example. Apache
You must specify the absolute path to the file you want to save. See Also: Creating / Writing a PHP File in the Destructor .
hakre source share