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
You made
$instance
private, which means it cannot be inherited. Change it toprotected
The manual discusses the individual uses of visibility
http://php.net/manual/en/language.oop5.visibility.php
So it looks like the hangup is in your edit
This will never be true.
getInstance
gets an instance of the current class. Since they are different classes they are not the same. Doing a test like this is sloppy, however. I would never do a test on an object like this. What would make more sense is to make sure that you're getting an instance ofSingleton
which can be done very easily like soBecause the child inherits the parent, it is an instance of the parent
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
toabstract
change your
$instance
variable to array$instance(s)
Now change
getInstance()
method like belowAnd change your test
You should write:-
You do not need to create child class of Singleton class. You can simply use below syntax to get object of Singleton class.
Simply define a method in any class which call
Singleton::getInstance();
and return the object.Check this link for more detail.