0

I would like to call to a static method in such a way that class name and method name are variables.

Example:

class QQQ {
   public function www($x) {
      echo $x;
   }
}

$q = 'QQQ';
$w = 'www';

$q::$w(7); // this is what I am trying to do but it throws an error.

Thoughts?

daryqsyro
  • 157
  • 2
  • 9

1 Answers1

0

Just need to change

public function www($x) {

to

public static function www($x) {

Because, you are calling it by scope resolution operator :: so, it should be static OR you should change the way you are calling it

$test = new $q;

$test->$w(5);

Should work, depending on what you are trying to do with it.

viral
  • 3,724
  • 1
  • 18
  • 32
  • 1
    Can you please explain why? Directly calling QQQ::foo(7) does the work. But calling it $q::$w(7) says function name must be a string – daryqsyro Jun 28 '15 at 13:12