PHP Singleton设计模式继承错误 [英] PHP Singleton design pattern inheritance error
问题描述
从下面的php单身课程
<?php
class Singleton
{
/ **
* @var Singleton这个类的* Singleton *实例的引用
* /
private static $ instance;
/ **
*返回此类的* Singleton *实例。
*
* @return Singleton * Singleton *实例。
* /
public static function getInstance()
{
if(null === static :: $ instance){
static :: $ instance = new static ();
}
return static :: $ instance;
}
/ **
*受保护的构造函数,以防止通过此类外部的new运算符创建
* * Singleton *的新实例。
* /
protected function __construct()
{
}
}
我试图继承新的小孩类
class SingletonChild extends Singleton {
但是当我做测试
$ obj = Singleton :: getInstance();
$ obj_two = SingletonChild :: getInstance();
var_dump($ obj === Singleton :: getInstance()); // bool(true)
var_dump($ obj === $ obj_two); // false
我正在收到php致命错误。
PHP致命错误:未捕获错误:无法访问属性
SingletonChild :: $ instance
在PHP 7.0中继承单元格 c> class 在PHP 7.0中很难,但是您可以通过在
首先使你的 Singleton
class
到抽象
抽象类Singleton {
}
将您的 $ instance
变量更改为数组 $ instance(s)
private $ instances = [];
现在更改 getInstance()
public static function getInstance(){
if(!isset(self :: $ instances [static :: class ]){
self :: $ instances [static :: class] = new static();
}
return self :: $ instances [static :: class];
}
更改测试
记住现在你不能调用
Singleton :: getInstance()
由于抽象
class SingletonChild extends Singleton {
}
class SingletonChildTwo extends SingletonChild {
}
$ obj = SingletonChild :: getInstance();
$ obj_two = SingletonChildTwo :: getInstance();
var_dump($ obj === SingletonChild :: getInstance()); // true
var_dump($ obj === $ obj_two); //将 - > false
From php singleton class below
<?php
class Singleton
{
/**
* @var Singleton The reference to *Singleton* instance of this class
*/
private static $instance;
/**
* Returns the *Singleton* instance of this class.
*
* @return Singleton The *Singleton* instance.
*/
public static function getInstance()
{
if (null === static::$instance) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Protected constructor to prevent creating a new instance of the
* *Singleton* via the `new` operator from outside of this class.
*/
protected function __construct()
{
}
}
I am trying to inherit new child class
class SingletonChild extends Singleton {
}
but when I do testing
$obj = Singleton::getInstance();
$obj_two = SingletonChild::getInstance();
var_dump($obj === Singleton::getInstance()); // bool(true)
var_dump($obj === $obj_two); // false
I'm getting php fatal error.
PHP Fatal error: Uncaught Error: Cannot access property SingletonChild::$instance
Inheriting Singleton class
in PHP is difficult, event in PHP 7.0, but you can do this with some changes on your class to work.
first make your Singleton
class
to abstract
abstract class Singleton {
}
change your $instance
variable to array $instance(s)
private $instances = [];
Now change getInstance()
method like below
public static function getInstance() {
if (!isset(self::$instances[static::class]) {
self::$instances[static::class] = new static();
}
return self::$instances[static::class];
}
And change your test
remember now you can't call
Singleton:: getInstance()
due to abstract
class SingletonChild extends Singleton {
}
class SingletonChildTwo extends SingletonChild {
}
$obj = SingletonChild::getInstance();
$obj_two = SingletonChildTwo::getInstance();
var_dump($obj === SingletonChild::getInstance()); // true
var_dump($obj === $obj_two); // will -> false
这篇关于PHP Singleton设计模式继承错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!