0

I'm trying to call a static function with a varible name from a class.

The desired outcome:

class Controller extends Controller {
    public $model = 'ModelName';
    public function index() {
        $woot = $this->model::find('');
        var_dump($woot);
    }
}

This works:

$class = 'ClassName';
$object = $class::find($parameters);

This works too:

$class = new Model();
$object = $class::find($params);

I am trying to define the new class name inside the current class, and call find as a static function from the current model. Any ideas how it's possible, without creating a new object, using __set, or declaring a local variable in the function?

4

2 に答える 2

2

あなたは実際にそれを行うことはできません. $this->varクラス インスタンス内では、別のクラスを参照するために使用することはできません。ただし、それを別のローカル変数に割り当てて、それを機能させることはできます

public function index() {
    $var = $this->model;
    $woot = $var::find('');
    var_dump($woot);
}
于 2015-06-03T18:08:18.770 に答える
1

私はMachavityのメソッドを使用しますが、call_user_func()orを使用できますcall_user_func_array()

public function index() {
    $woot = call_user_func_array(array($this->model, 'find'), array(''));
}
于 2015-06-03T18:17:05.997 に答える