PHP: Use variable name to call static function on Singleton Object?

You should start by determining what it is that takes you into an infinite loop. Does your constructor ( someLibrary::__construct() ) have any code that directly/indirectly calls Library::getInstance() for example? EDIT get_called_class() was introduced in PHP 5.3, so if your code actually works, you're already running 5.3.

You should start by determining what it is that takes you into an infinite loop. Does your constructor (someLibrary::__construct()) have any code that directly/indirectly calls Library::getInstance(), for example? EDIT get_called_class() was introduced in PHP 5.3, so if your code actually works, you're already running 5.3.

– soulmerge Sep 8 '09 at 12:52 THAT edit :P I really should comment AFTER I make the changes... – mynameiszanders Sep 8 '09 at 12:57 Oh, ok (I really hate this min 15 chars limitation on comments) – soulmerge Sep 8 '09 at 13:02.

You could try to solve this with eval(). To get you an idea: $theVar = "relvantClassName"; $someObject = eval($theVar::getInstance()); $result = $someObject->performAction().

I need to call a static function from an object using the Singleton design, but using a variable as the class name. The best way, $class::getInstance();, is only available in PHP 5.3, and the other way I found, call_user_func(array($class, 'getInstance'));, results in the maximum execution time being breached. Does anyone know why this is happening, or of a way for this to work / a workaround?

I know that this is not the best way for things to be done, and the Singleton design pattern would not be my first choice, but unfortunately it's not up to me. Soulmerge make a valid point saying that get_called_class() is only in PHP 5.3, and therefore I must be using it, but alas, I just cheat my way round things as I usually do (Thanks to Chris Webb from http://www.septuro.com/ for the code - far too complex to be any of my own!). I shall go over all my code again, as there must be a loop somewhere.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions