What if I wanted to create a hierarchy of classes that basically would act a one singleton. For example... I have an http_response class. It has a child called front_http_response which is used by my front controller to resolve pretty urls.
Now, this is purely curiousity... is it possible to make it so that if any child of the http_response class is instantiated, all others are unable to be instantiated. So it would be a singleton, but a much bigger one... I guess. Does that make sense?
Singletons and inheritance
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
I can see it both ways.The Ninja Space Goat wrote:practical? useless?
Read here for some ideas:The Ninja Space Goat wrote:Another question... with a singleton... I always have a hard time setting constructer arguments, because I don't like having to repeat the arguments from getInstance to __construct. Is there any way around this?
viewtopic.php?t=53444
viewtopic.php?t=53274
registry, use an instanceof check in the registry when calling a new instance. Add an error of "DENIED!!1" for comedic hollywood esque comedy, and tada!
you could even make the singleton in the parent class and specify the child you wish to create and do all the above there.
you could even make the singleton in the parent class and specify the child you wish to create and do all the above there.
Code: Select all
class ParentController
{
private static instance;
private static children = array('ParentController', /* allowed kids go here */);
public static getInstance ($child = 'ParentController')
{
if (in_array($child, self::$children)) {
if (!isset(self::$instance)) self::$instance = new $child;
return self::$instance;
} else {
/* invalid child specified */
}
}
/* etc.. */
}