for the "Late Static Bindings" imported by php from version 5.3,([url]http://php.net/manual/en/language.oop5.late-static-bindings.php[/url])
singleton patterns:
a code-less but confusable style:
the following maybe the best one:
singleton patterns:
<?php
class Singleton {
static private $instance;
protected static function create(){
if(is_null(self::$instance)){
self::$instance = new self();
}
return self::$instance;
}
static public function instance(){
return static::create(); //!important to use static
}
public function __construct(){echo 1;}
}
class MySingleton extends Singleton {
static private $instance; //in subclass,explicitly declare this and the method below
protected static function create(){
if(is_null(self::$instance)){
self::$instance = new self();
}
return self::$instance;
}
}
class YourSingleton extends Singleton {
static private $instance;
protected static function create(){
if(is_null(self::$instance)){
self::$instance = new self();
}
return self::$instance;
}
}
class ThirdSingleton extends MySingleton {
static private $instance;
protected static function create(){
if(is_null(self::$instance)){
self::$instance = new self();
}
return self::$instance;
}
}
echo get_class(Singleton::instance()); //=>Singleton
echo get_class(MySingleton::instance()); //=> MySingleton
echo get_class(MySingleton::instance()); //=> MySingleton
echo get_class(YourSingleton::instance()); //=>YourSingleton
echo get_class(ThirdSingleton::instance()); //=>ThirdSingleton
echo get_class(ThirdSingleton::instance()); //=>ThirdSingleton
a code-less but confusable style:
<?php
class Singleton {
static private $instance;
static public function instance(){
static $instance = null; //should not add self:: or static:: here
return $instance ?: $instance = new static;
}
public function __construct(){echo 1;}
}
class MySingleton extends Singleton {
}
class YourSingleton extends Singleton {
}
class ThirdSingleton extends MySingleton {
}
echo get_class(Singleton::instance()); //=>Singleton
echo get_class(MySingleton::instance()); //=> MySingleton
echo get_class(MySingleton::instance()); //=> MySingleton
echo get_class(YourSingleton::instance()); //=>YourSingleton
echo get_class(ThirdSingleton::instance()); //=>ThirdSingleton
echo get_class(ThirdSingleton::instance()); //=>ThirdSingleton
the following maybe the best one:
class Singleton {
static protected $instance; //should not be private
static public function instance(){
if(is_null(static::$instance)){ //important to use static
static::$instance = new static();
}
return static::$instance;
}
public function __construct(){echo 1;}
}
class MySingleton extends Singleton {
static protected $instance; //must explicitly declared
}
class YourSingleton extends Singleton {
static protected $instance;
}
class ThirdSingleton extends MySingleton {
static protected $instance;
}
echo get_class(Singleton::instance()); //=>Singleton
echo get_class(MySingleton::instance()); //=> MySingleton
echo get_class(MySingleton::instance()); //=> MySingleton
echo get_class(YourSingleton::instance()); //=>YourSingleton
echo get_class(ThirdSingleton::instance()); //=>ThirdSingleton
echo get_class(ThirdSingleton::instance()); //=>ThirdSingleton
本文详细解析了PHP中Singleton模式的实现方式,特别是利用Late Static Binding的技巧,通过实例展示了如何创建和使用单例类,并讨论了不同实现方式下的类实例获取过程。
139

被折叠的 条评论
为什么被折叠?



