2

I have the "foundations" of a web site that can be extended to create specific applications. In my core features I have an abstract class C_Controller which has to be a singleton. In order to create a specific application using the core features, a developer has to extend this C_Controller class. This class (let's name it C_Application) also has to be a singleton. In order to force the developer to implement his own getInstance method, I've done the following :

interface I_Singleton
{
    public static function getInstance();
}

abstract class C_Controller implements I_Singleton
{
    public abstract static function getInstance();
}

Then the developer would have to implement the function in his sub class:

class C_Application extends C_Controller
{
    public static function getInstance() { ... }
}

I've already been doing this for my views, but it was a non-static method (__toString). I've read that it's not possible to have abstract static methods since PHP 5.2, so is there a clean way to do that?

I'm limited to PHP 5.2. due to the free web hosting service I'm using.

Thanks for your help!

jasonlam604
  • 1,386
  • 1
  • 13
  • 23
Virus721
  • 7,156
  • 8
  • 49
  • 110
  • If you want `C_Controller` to be singleton, it should implement `getInstance` and your code should call `C_Controller::getInstance` to access the object. That's one of the most important points of singleton: accessing an object of a derived class through a static method of its base class. – Oswald Mar 31 '13 at 10:14
  • Don't use singletons. They are [bad](http://stackoverflow.com/a/138012/142019). Remember that you don't need to put everything in a class; free functions are fine. –  Mar 31 '13 at 15:13
  • 2
    Yeah, i could also code my whole website in assembly, it would work after all. II want to use a singleton because i want a dumb-proof coding environment :) – Virus721 Apr 01 '13 at 13:19

0 Answers0