PHP Singleton Design Pattern-Vererbungsfehler
Von PHP Singleton-Klasse unten
<?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()
{
}
}
Ich versuche, eine neue Kinderklasse zu erben
class SingletonChild extends Singleton {
}
aber wenn ich teste
$obj = Singleton::getInstance();
$obj_two = SingletonChild::getInstance();
var_dump($obj === Singleton::getInstance()); // bool(true)
var_dump($obj === $obj_two); // false
Ich erhalte einen schwerwiegenden PHP-Fehler.
PHP Schwerwiegender Fehler: Nicht abgefangener Fehler: Kein Zugriff auf die Eigenschaft SingletonChild :: $ instance