How to emulate __destruct () in a static class?

I have encoded a simple configuration class for my own structure.

There are simple functions like get() , set() or loadFile() . But all functions and variables are static.

And now I want to implement an autosave mechanism. I had an idea to create an instance (in my init () function), whose __destruct() will call the static destruct() function:

 <?php class Config { static private $autoSave; static public function get() {} /* set(), save(), load(), etc. */ static public function init($autoSave) { self::$autoSave = $autoSave; new Config(); } static public function destruct() { if (self::$autoSave) self::save(); } public function __destruct() { Config::destruct(); } } ?> 

Are there any better solutions or is my design template completely wrong in this case?

+6
source share
4 answers

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() {} /* set(), save(), load(), etc. */ 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 .

+10
source

Inside your init method, add a register_shutdown_function call:

 register_shutdown_function(array('Config', 'destruct')); 
+6
source

Have you looked register_shutdown_function ? You can add your method to the shutdown part of the script.

You could also look at the Singleton template .

+3
source

You can instantiate this static class in autoregister.

 $instance = array(); spl_autoload_register(function ($class) { ... global $instance; if ($isStatic) $instance[] = new $class(); ... }); 

This works great for me.

... and for those who donโ€™t like the readable code (it is not tested):

 class staticInstances() { private static $list = array(); public static function add($class) { self::$list[] = new $class(); } function __distruct() { foreach (self::$list as $class) unset(self::$list); } } $staticInstances = new staticInstances(); spl_autoload_register(function ($class) { ... if ($isStatic) staticInstances::add($class); ... }); 
0
source

Source: https://habr.com/ru/post/894554/


All Articles