0

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 } } 
5
  • 2
    You asked us to let you know if this is confusing. It's confusing. Commented Feb 15, 2012 at 23:07
  • okay okay, i posted code snippets :) Commented Feb 15, 2012 at 23:09
  • What do you need the normal class for and why extend it with a singleton pattern? Commented Feb 15, 2012 at 23:10
  • the normal class is a templating class and i want to extend it to use it's many useful functions through my singleton. Commented Feb 15, 2012 at 23:12
  • What should the singleton do? Can you show some more code? Commented Feb 15, 2012 at 23:15

2 Answers 2

2

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'); 
Sign up to request clarification or add additional context in comments.

3 Comments

this is pretty good, the only problem is that there are a lot more functions than just get and set and I can't just implement them all in the singleton thus the extending.
Your not "implementing them all in the singleton", the singleton represents a single instance of class3, which means that it has access to EVERY public member variable and function it would as if it extended class3.
oh wait, i take back what i've said. I didn't notice that your solution was class3 and not the singleton.
0

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"); 

4 Comments

Be careful, you will end up with namespace collisions if you opt to do the same thing for another class.
What if you wanted to do the same thing for class2? You will now have two classes named Singleton, which will cause errors due to namespace collision. I'd recommend going with my solution, so that you avoid this issue in the future.
thanks, that makes sense. although it will never be used this way
No worries, just making you aware of possible future issues. Good luck.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.