0

「call_user_func」を使用せず、eval を使用せずにクラス内の関数を呼び出す方法はありますか?

これが私が尋ねる理由です:

「call_user_func」を使用すると、「$this not in context」エラーが発生します。

$this->id = $selectId['id'];
$file = 'score_' . $this->sid . '.php'; //the file with the class

if (@include_once($file)) { //including the file

    $scoreClass = 'Score_' . $this->id;
    call_user_func($scoreClass .'::selectData', $this->user_id);
}

この「コンテキスト内ではない」エラーを次のように関数を呼び出すことで解決できますが、私の名前は変数ではありません。

$this->id = $selectId['id'];
$file = 'score_' . $this->sid . '.php'; //the file with the class

if (@include_once($file)) { //including the file

    $test = new Score_98673();
    $test->selectData($this->user_id);

}
4

2 に答える 2

3
call_user_func(array($score, 'selectData'), $this->user_id);

これは疑似型の正しい構文ですcallable
http://php.net/manual/en/language.types.callable.php

ところで、これを行うこともできます:

$method = 'selectData';
$score->$method($this->user_id);
于 2013-04-16T13:25:40.600 に答える
1
if (@include_once($file)) { //including the file
    $scoreClass = 'Score_' . $this->id;
    $test = new $scoreClass();
    $test->selectData($this->user_id);
}

必要な操作はこれだけです。を使用するときに変数クラス名を使用できるようにnew

を使用する非静的メソッドを呼び出すと(エラーが発生するはずです)、上記は失敗します$this

于 2013-04-16T13:22:37.310 に答える