LoginSignup
0
0

More than 5 years have passed since last update.

同じfunctionを何度も呼び出してるコードを可変個引数関数と...(スプレッド演算子)による整理

Posted at

※本記事はphp5.6~で動作する内容になります。

引数違いの同じfunctionが複数書かれているものを少しスマートにした話です。
必ずこれにする必要は当然ありませんが、1つの方法としてこういったことができます。

元のコード.php

mixJuice('Apple');
mixJuice('Strawberry');
mixJuice('Watermelon');
mixJuice('Guava');

function mixJuice($arg)
{
  ※※※ 処理 ※※※
}

リファクタリングしたコード.php
mixJuice('Apple', 'Strawberry', 'Watermelon', 'Guava');

function mixJuice(...$args)
{
    for($i = 0; $i < count($args); $i++) {
        ※※※ 処理 ※※※
    }
}
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