在 PHP 中创建单例时,可以使用抽象类而不是私有 __construct() 吗?
在 PHP 中创建 Singleton 时,我通过执行以下操作来确保它无法实例化:
class Singleton {
private function __construct() {}
private function __clone() {}
public static function getInstance() {}
}
但是,我意识到将类定义为“抽象”意味着它无法实例化。那么,执行以下操作有什么问题吗:
abstract class Singleton {
public static function getInstance() {}
}
第二种情况允许我编写更少的代码行,这会很好。 (并不是说它实际上有很大的不同。)
When creating a Singleton in PHP, I ensure that it cannot be instantiated by doing the following:
class Singleton {
private function __construct() {}
private function __clone() {}
public static function getInstance() {}
}
However, I realised that defining a class as 'abstract' means that it cannot be instantiated. So is there anything wrong with doing the following instead:
abstract class Singleton {
public static function getInstance() {}
}
The second scenario allows me to write fewer lines of code which would be nice. (Not that it actually makes much of a difference.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 PHP 中创建单例时,将 __construct 和 __clone 声明为私有可确保该类无法从外部实例化:它仍然可以从其声明中实例化。
当将一个类声明为
抽象
时,它根本无法实例化;甚至从其声明中也没有。这意味着您的解决方案将不起作用:在第二种情况下,您的 getInstance() 方法将无法实例化该类,而在第一种情况下它可以这样做。
When creating a singleton in PHP, declaring the
__construct
and__clone
as private ensures that the class cannot be instanciated from the outside : it can still be instanciated from inside its declaration.When declaring a class as
abstract
, it can not be instanciated at all ; not even from inside its declaration.This means your solution would not work : in the second case, your
getInstance()
method will not be able to instanciate the class -- while it can do so in the first case.不可以,因为那样你就根本无法实例化该类(即使在静态 getInstance 方法中也无法实例化)。单例示例中的私有构造函数只是确保只有来自同一类的静态 getInstance 方法才能访问该构造函数。
No because then then you can't instantiate the class at all (not even in the static getInstance method). The private constructor in the singleton example just assures, that only the static getInstance method from the same class can access the constructor.
不可以,创建单例时不能使用抽象类来代替私有 __construct()。但是,如果您的目的是创建一个从中进行扩展的抽象单例,您可以这样做:
然后您可以像这样从单例扩展:
和
并操作:
但是,请记住单例很难进行单元测试并应尽可能避免。请参阅我的回答以了解一些背景:
No, you cannot use an abstract class instead of a private __construct() when creating a singleton. But if your intention is to create an Abstract Singleton from which to extend from, you can do so like this:
You can then extend from Singleton like this:
and
and manipulating:
However, keep in mind that Singletons are very hard to unit-test and should be avoided if possible. See my answer here for some background:
如果您的
Singleton::getInstance()
应该返回不同类的实例,那么它就可以工作。但我会发现这令人困惑。有点像滥用抽象来将抽象工厂的复杂性与单例的限制结合起来。
It could work if your
Singleton::getInstance()
is supposed to return an instance of a different class.But I'd find that confusing. A bit like misusing
abstract
to combine the complexity of an abstract factory with the restraints of a singleton.