Page 1 of 1

obtaining instance of a singleton object with variable name

Posted: Wed Jun 14, 2006 11:01 pm
by bg
The following works to instantiate a class with a variable name

Code: Select all

$class_name = 'blah';

$newclass = new $class_name();
I want to do something similar, except I want to get an instance of a singleton object. The following code, obviously, throws an error

Code: Select all

$class_name = 'blah';

$newclass = $class_name::instance();
The way I worked around this follows

Code: Select all

$load_driver = create_function('','if(!$retval = '.$driver_name.'::instance()){return false;} return $retval;');
However this is incredibly slow because its a user created function. If you know how to do this without a hack id like to hear it.

Posted: Wed Jun 14, 2006 11:09 pm
by feyd
from earlier: viewtopic.php?t=50097

Posted: Wed Jun 14, 2006 11:13 pm
by Christopher
You might want to take a look at the Registry pattern.

Posted: Fri Jun 16, 2006 4:47 pm
by bg
feyd wrote:from earlier: viewtopic.php?t=50097
Thank you sir.