PHP - call to a variable static method using the scope resolution operator

544 Views Asked by At

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?

1

There are 1 best solutions below

1
On

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.