0

Why, in a class instance context, don't calls of the form $this->className::staticMethod work, but calls of the form $className::staticMethod do work?

In the example below callDoSomething2 works, but callDoSomething does not work (I get a parser error). I'm using PHP version 5.3.15.

<?php
class A {
    private $className;

    public function __construct($className) {
        $this->className = $className;
    }

    public function callDoSomething() {
        $this->className::doSomething();
    }

    public function callDoSomething2() {
        $className = $this->className;
        $className::doSomething();
    }
}

class B {
    public static function doSomething() {
        echo "hello\n";
    }
}

$a = new A('B');
$a->doSomething();
maxenglander
  • 3,991
  • 4
  • 30
  • 40

1 Answers1

1

callDoSomething2 is one way to do it, the other would be to use something along the lines of

call_user_func("{$this->className}::doSomething");
sgrif
  • 3,702
  • 24
  • 30