克隆如何创建新版本的单例?
How can a clone create a new version of a singleton?
我问了问题,得到了很好的解决方案。我了解如何在没有 new
的情况下创建对象(我认为这是构造对象的唯一方法)。
我了解到,通过创建单例 class 我们强制 class 仅实例化一次。
// a simple singleton class.
class Singleton
{
private static $object = null;
private function __construct() {}
public static function createObject()
{
if (self::$object == null) {
self::$object = new Singleton();
}
return self::$object;
}
}
//$obj = new Singleton(); Obviously an error
$obj1 = Singleton::createObject();
// object(Singleton)[1]
$obj2 = Singleton::createObject();
// object(Singleton)[1]
// $obj1 and $obj2 are same. Both have same id = 1
$obj3 = clone $obj1;
// object(Singleton)[2]
// $obj3 is a new instantiate. id = 2
这是怎么回事? clone
在这里如何工作?
如果你想防止克隆单例添加私有方法__clone private function __clone() {}
.
I learned that by creating a singleton class we force a class to instantiate only once.
是的,如果我们使用我们实际设计的方法来创建对象。尽管单例的想法是在整个应用程序中共享对象的单个实例,但它仍然是对象的 实例。
因此,可以随意克隆对象的实例。没有什么能阻止它被克隆。阻止复制实例的是使用 createObject()
方法。 (为了记录,最好命名为 getObject()
)
所以顺序是:
$obj1 = Singleton::createObject();
// the method call creates the instance with new
// assigns it to the property of the singleton class
// then returns it
$obj2 = Singleton::createObject();
// the method returns the instance from the first method call
// as it is still defined in the static property
// BOTH $obj1 and $obj2 are the same instance of an object
$obj3 = clone $obj1;
// here, we use the clone keyword.
// this has nothing to do with our singleton, yet
// it simply clones an instance of an object.
// $obj1 is an instance of an object. It is therefore cloned.
现在,如果您真的希望克隆到 return 同一个实例,您可以如 MaxP 所述,在 Singleton
class 中定义 __clone()
魔术方法.
function __clone()
{
return self::createObject();
}
哪个 return 是正确的实例,就像对 createObject()
.
的任何其他调用一样
我问了new
的情况下创建对象(我认为这是构造对象的唯一方法)。
我了解到,通过创建单例 class 我们强制 class 仅实例化一次。
// a simple singleton class.
class Singleton
{
private static $object = null;
private function __construct() {}
public static function createObject()
{
if (self::$object == null) {
self::$object = new Singleton();
}
return self::$object;
}
}
//$obj = new Singleton(); Obviously an error
$obj1 = Singleton::createObject();
// object(Singleton)[1]
$obj2 = Singleton::createObject();
// object(Singleton)[1]
// $obj1 and $obj2 are same. Both have same id = 1
$obj3 = clone $obj1;
// object(Singleton)[2]
// $obj3 is a new instantiate. id = 2
这是怎么回事? clone
在这里如何工作?
如果你想防止克隆单例添加私有方法__clone private function __clone() {}
.
I learned that by creating a singleton class we force a class to instantiate only once.
是的,如果我们使用我们实际设计的方法来创建对象。尽管单例的想法是在整个应用程序中共享对象的单个实例,但它仍然是对象的 实例。
因此,可以随意克隆对象的实例。没有什么能阻止它被克隆。阻止复制实例的是使用 createObject()
方法。 (为了记录,最好命名为 getObject()
)
所以顺序是:
$obj1 = Singleton::createObject();
// the method call creates the instance with new
// assigns it to the property of the singleton class
// then returns it
$obj2 = Singleton::createObject();
// the method returns the instance from the first method call
// as it is still defined in the static property
// BOTH $obj1 and $obj2 are the same instance of an object
$obj3 = clone $obj1;
// here, we use the clone keyword.
// this has nothing to do with our singleton, yet
// it simply clones an instance of an object.
// $obj1 is an instance of an object. It is therefore cloned.
现在,如果您真的希望克隆到 return 同一个实例,您可以如 MaxP 所述,在 Singleton
class 中定义 __clone()
魔术方法.
function __clone()
{
return self::createObject();
}
哪个 return 是正确的实例,就像对 createObject()
.