雷锋 发表于 2017-3-23 11:40:54

php 5.3 subclass singleton

for the "Late Static Bindings" imported by php from version 5.3,(http://php.net/manual/en/language.oop5.late-static-bindings.php)
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
页: [1]
查看完整版本: php 5.3 subclass singleton