Singleton class name as variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pata kusik
    New Member
    • Sep 2011
    • 1

    Singleton class name as variable

    I have a question about singleton design pattern and name of class as variable.

    I have a config file with value like this:
    $config["database_class _name"]="MyProjectData baseHandler"

    Then I aslo have class MyProjectDataba seHandler, witch is Singleton, where i handle everything connected with database

    And finally I have second class, and I would like to work it like this:

    class MyClass{

    public function foo(){
    $GLOBALS["config"]["database_class _name"]::get_instance( )->query($query ); //should work same as: MyProjectDataba seHandler::get_ instance()->query($query )
    }
    }

    But it doesn't work. May I ask for a right way how to write a name of singleton class as variable?
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    What you need is to create an instant of a class, and you want the class name from variable????

    PHP support it.
    Simplest Example is
    Code:
    class MyClass
    {
     function foo()
     {
      echo __LINE__."<br>";
     }
    };
    $ClassName="MyClass";
    $Object=new $ClassName;
    $Object->foo();

    Comment

    Working...