Molot Posted July 6, 2007 Share Posted July 6, 2007 I want to implement "autosingleton" - abstract class, that makes singleton from everything that extends it. It means that the abstract singleton class must know the name of an actual (last in hierarchy) class to create it's single instance when requested for the first time. I can't find how to do it. The main problem is that I want to avoid adding any extra code to the child itself. If I'd decided to add code to the children manually, I could just as well (and simpler) paste that few lines which make a singleton from a class, without any inheritance. Quote Link to comment https://forums.phpfreaks.com/topic/58676-knowing-last-class-name-in-a-parent-class/ Share on other sites More sharing options...
Daniel0 Posted July 6, 2007 Share Posted July 6, 2007 class_parents() perhaps? Quote Link to comment https://forums.phpfreaks.com/topic/58676-knowing-last-class-name-in-a-parent-class/#findComment-291132 Share on other sites More sharing options...
Molot Posted July 6, 2007 Author Share Posted July 6, 2007 No. Reverse. I'm looking for something like class_childs() I need the name of a child in a scope of a parent. Singleton would be a parent, and it has to know instance of what (child) he needs to create. Sorry if my previous post was ambiguous or misinforming. Quote Link to comment https://forums.phpfreaks.com/topic/58676-knowing-last-class-name-in-a-parent-class/#findComment-291267 Share on other sites More sharing options...
448191 Posted July 9, 2007 Share Posted July 9, 2007 What you are asking is impossible. A parent class has no knowledge of it's children. It is possible to get the name of the class lowest in the hierarchy (for example using get_class($this)), but only at runtime. You'd need to instantiate an object first, so this is useless for use with the Singleton pattern. It is simply not possible to have a generic Singleton abstract class. Helas. Quote Link to comment https://forums.phpfreaks.com/topic/58676-knowing-last-class-name-in-a-parent-class/#findComment-293116 Share on other sites More sharing options...
Molot Posted July 9, 2007 Author Share Posted July 9, 2007 What you are asking is impossible.In php only, as far as I can see... It is simply not possible to have a generic Singleton abstract class. Helas. In c++ and java it is perfectly all right and possible, either by inheritance or by a template wrapper... It's kinda shame it's impossible in php, don't you think? To be honest, I hope you are wrong and there is - or at least will be - some way to do this cleanly. Quote Link to comment https://forums.phpfreaks.com/topic/58676-knowing-last-class-name-in-a-parent-class/#findComment-293185 Share on other sites More sharing options...
448191 Posted July 9, 2007 Share Posted July 9, 2007 In this case, I would like to be wrong. I would like a generic Singleton superclass as much as the next guy. But, when you think about it, it wouldn't be much use anyway. Even if you where able to get the right class name in the scope of the parent, you would have to declare the constructor public, or the parent won't be able to instantiate the child. This is the closest one can come to what you want: <?php abstract class Singleton { private static $instances = array(); protected static function getClassInstance($class) { if (!isset(self::$instances[$key])) { self::$instances[$key] = new $class(); } return self::$instances[$key]; } abstract function getInstance(); } class ConcreteSingleton extends Singleton { public function __construct(){} public static function getInstance(){ return Singleton::getClassInstance('ConcreteSingleton'); } } ?> But it quickly resembles more of a global registry than the singleton pattern, and isn't very useful.. One could use the backtrace to confirm the constructor is called from the Singleton superclass, but it's all more trouble than it's worth. Quote Link to comment https://forums.phpfreaks.com/topic/58676-knowing-last-class-name-in-a-parent-class/#findComment-293208 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.