【发布时间】:2015-01-01 20:15:00
【问题描述】:
我正在阅读有关 OOP 中的模式的内容,并遇到了单例模式的以下代码:
class Singleton
{
/**
* @var Singleton reference to singleton instance
*/
private static $instance;
/**
* gets the instance via lazy initialization (created on first usage)
*
* @return self
*/
public static function getInstance()
{
if (null === static::$instance) {
static::$instance = new static;
}
return static::$instance;
}
/**
* is not allowed to call from outside: private!
*
*/
private function __construct()
{
}
/**
* prevent the instance from being cloned
*
* @return void
*/
private function __clone()
{
}
/**
* prevent from being unserialized
*
* @return void
*/
private function __wakeup()
{
}
}
有问题的部分是static::$instance = new static;。 new static 到底是做什么的或者这个例子是如何工作的。我熟悉您的平均new Object,但不熟悉new static。任何对 php 文档的引用都会有很大帮助。
【问题讨论】: