Reputation: 1196
I have a singleton class that extends a normal class. (Class1)
The normal class has two non-static functions called set() and get(). (Class2)
A third class (Class3) which gets the singleton's (Class1's) instance uses the set() and get() through the singleton. It works fine using Class3, but is there any way to use the get() method inside the singleton from the parent class to see what the "third" class set it to?
I can't seem to call the get because it's non static. Please let me know if this is confusing.
class Class1 {
public function doThings(){
$this->view->set("css","1234");
}
}
class Singleton extends Class3 {
static public function instance()
{
if (!self::$_instance instanceof self) {
self::$_instance = new self();
}
return self::$_instance;
}
//I want this singleton to call get("css") and have it return the value.
}
class Class3{
public function get(arg){//implementation }
public function set(arg){//implementation }
}
Upvotes: 0
Views: 488
Reputation: 1196
I just solved my own question, sorry for the late reply.
All I had to do in the singleton was call get() this way:
self::instance()->get("css");
Upvotes: 0
Reputation: 19979
I've never seen specific class just for a singleton which extends the class it is supposed to be a singleton for. Instead, try this:
class Class3
{
private $_instance;
static public function instance()
{
if (!self::$_instance instanceof self) {
self::$_instance = new self();
}
return self::$_instance;
}
public function get(arg){//implementation }
public function set(arg){//implementation }
}
// Calling code
// I want this singleton to call get("css") and have it return the value.
Class3::getInstance()->set('css', 'border:0');
Class3::getInstance()->get('css');
Upvotes: 2