LoginSignup
0
0

More than 1 year has passed since last update.

関数やメソッドの引数の数を調べて、必須項目以外をnullで埋める際のtips

Last updated at Posted at 2021-08-31
class Hoge
{
    // 引数が多い
    public function getHogehoge(int $id, ?int $arg1, ?int $arg2, ?int $arg3, ?int $arg4, ?int $arg5)
    {
        return 'hogehoge';
    }
}

こんな引数が多いメソッド(しかも引数の数はたまに変わる)を使う際、$idだけ使い、それ以外nullで埋める場合の備忘録
そもそも引数をどうにかしろという言葉はぐっと堪えて下さい。。。

対応

// 関数の構造を格納
$refl = new \ReflectionClass("Hoge(HogeクラスのあるPath)");
// getHogehogeの引数の数を取得
$arg_num = $refl->getMethod("getHogehoge")->getNumberOfParameters();

$hoge = new Hoge();
// array_fillでnullを$arg_num - 1個分埋める
$products = $hoge->getHogehoge(1, ...array_fill(0, $arg_num - 1, null));

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0