0

クラスでいくつかの関数を呼び出すために使用するこの小さな関数があります。

public static function call($method){
    $Model = new Model();
    if(method_exists($Model, $method)){
        return $Model->{$method}();
    }
}

さて、私の質問は、渡された引数についてです。それらを再渡ししたいのですが、配列を渡すのではなく、実際の引数を渡します。

関数 func_get_arg() と func_num_args() は知っていますが、これは機能しません:

$args = '';
for($i=0; $i<func_num_args(); $i++){
    $args .= func_get_args($i).',';
}

$args = substr($args, 0, strlen($args)-1);

$Model->{$method}(passed_args) を呼び出して渡すことができる代替メソッドはありますか?

アップデート

メソッドをこれに変更しようとしましたが、うまくいきません:

public static function call($method){
    $Model = new Model();

    $args = func_get_args();
    if(method_exists($Model, $method)){
          return call_user_func_array(array($Model, $method), $args);
    }
}

今まで引数が1つしかないか、まったくないため、これを行うと機能します。

public static function call($method, $args = null){
    $Model = new Model();

    if(method_exists($Model, $method)){
        return $Model->{$method}($args);
    }
}

解決:

もちろん、メソッド呼び出しを変更する必要があります。

public static function call(){
    $Model = new Model();

    $args = func_get_args();
    $method = array_shift($args);

    if(method_exists($Model, $method)){
        return call_user_func_array(array($Model, $method), $args);
    }
}

上記の作品。ありがとうございました。

4

1 に答える 1