【发布时间】:2013-02-23 22:17:47
【问题描述】:
我想知道的问题可能更多是风格问题或您有什么问题。我在 PHP 中有一个单例类。当我检查对象是否已经创建时,我只是这样做:
if(self::$instance == null)
self::$instance = new self();
但是,我已经看到了一些 PHP 中单例模式的实现 !isset:
if(!isset(self::$instance))
self::$instance = new self();
据我了解,这两个语句在功能上没有区别。一种方式比另一种更好吗?一种方式比另一种更专业吗?
编辑:
这是我的单例类中整个模式的代码:
private static $instance = null;
private function __construct() { }
public static function getInstance() {
if(self::$instance == null) {
self::$instance = new self();
}
return $instance;
}
【问题讨论】: