0

I'm in PHP and I must access a Static method of an object which name must change.

   private $controlleur = null;
   private static $instance = null;

   private function __construct() {
     $nomControlleur = "Controlleurs\_" . Session::singleton()->controlleur;
     $this->controlleur = $nomControlleur::singleton();
   }

This preceding code is giving me " Syntax error unexpected :: ".
I've also tried writing {$nomControlleur}::singleton(); but it's giving me even more errors, thanks a lot for your help.

Balls of steel

3 Answers 3

2

Use:

$this->controlleur = call_user_func(array($nomControlleur, 'singleton'));

or (5.2.3+ only)

$this->controlleur = call_user_func($nomControlleur . '::singleton');
Sign up to request clarification or add additional context in comments.

Comments

0

What about

$staticCall = $nonController."::singleton()";
$staticCall();

?

1 Comment

Thanks, nearly that, but I have find it with your comment. Just pull of the () in singleton() beacause when you call it it gives singleton()();
0

I think now in PHP 5.3 there is also PHP __callStatic() as there was __call().

So you can define __callStatic() and undefined static method call will hit this method.

Please check here for more details and usage.

http://www.php.net/manual/en/language.oop5.overloading.php#object.callstatic

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.