如何在静态类中模拟 __destruct() ?
我为我自己的框架编写了一个简单的配置类。
有一些简单的函数,例如 get()
、set()
或 loadFile()
。 但所有函数和变量都是静态的。
现在我想实现一个自动保存机制。我想创建一个实例(在我的 init() 函数中),其 __destruct()
将调用静态 destruct()
函数:
<?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();
}
}
?>
是否有更好的解决方案或者是在这种情况下我的设计模式完全错误吗?
I've coded a simple configuration class for my own framework.
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 the 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 pattern completely wrong in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
析构函数仅在对象上调用,而不是在静态类上调用。
相反,您可以将类从静态转换为常规类,以便创建它的实例。然后它就会有析构函数。此外,它还使您的代码更易于重用和测试。
此外,您还可以为
__get
和__set
或ArrayAccess
实现魔术方法,这通常对于轻松数据存储和访问以及配置很有用。或者,您可以将析构函数对象添加到静态类成员以实现您正在寻找的功能:
仅供参考:您写道您想要添加自动保存功能。
__destruct()
和register_shutdown_function
存在一个常见的差距>:您应该指定一个绝对路径来访问要保存到的文件。另请参阅:在析构函数中创建/写入 PHP 文件。
Destructors are called on objects only, not for static classes.
Instead you could convert your class from static to regular so you can create an instance of it. Then it will have the destructor. Additionally it makes your code easier to re-use and test.
Additionally you're able to implement magic methods for
__get
and__set
orArrayAccess
which often is useful for easy data storage and access as for a configuration.Alternatively, you can add a destructor object to a static class member to achieve what you're looking for:
Just FYI: You wrote you want to add an auto-save functionality. There is a common gap to fall over for both
__destruct()
andregister_shutdown_function
:You should specify an absolute path to access the file you want to save into. See as well: PHP file creation/write within destructor.
在
init
方法中,添加对register_shutdown_function
的调用:Inside your
init
method, add a call toregister_shutdown_function
:您看过 register_shutdown_function 吗?您可以将您的方法添加到脚本的关闭部分。
查看单例模式也可能是值得的。
Have you looked at register_shutdown_function? You could add your method to the shutdown part of the script.
It could also be worth it to look at the Singleton pattern.
您可以在自动注册时创建此静态类的实例。
这对我来说效果很好。
...对于那些不喜欢可读代码的人(未经测试):
You can create an instance of this static class on autoregister.
This is working fine for me.
... and for those who don't like readable code (it is untested):