Jump to content

Knowing last class name in a parent class?


Molot

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.