8

私はこれを持っています:

function foo($a='apple', $b='brown', $c='Capulet') {
    // do something
}

このようなことは可能ですか:

foo('aardvark', <use the default, please>, 'Montague');
4

6 に答える 6

12

それがあなたの関数である場合はnull、ワイルドカードとして使用して、後で関数内でデフォルト値を設定できます。

function foo($a=null, $b=null, $c=null) {
    if (is_null($a)) {
        $a = 'apple';
    }
    if (is_null($b)) {
        $b = 'brown';
    }
    if (is_null($c)) {
        $c = 'Capulet';
    }
    echo "$a, $b, $c";
}

次に、次を使用してそれらをスキップできますnull

foo('aardvark', null, 'Montague');
// output: "aarkvark, brown, Montague"
于 2009-02-23T21:13:24.230 に答える
5

それが PHP のコアの 1 つではなく、独自の関数である場合は、次のようにすることができます。

function foo($arguments = []) {
  $defaults = [
    'an_argument' => 'a value',
    'another_argument' => 'another value',
    'third_argument' => 'yet another value!',
  ];

  $arguments = array_merge($defaults, $arguments);

  // now, do stuff!
}

foo(['another_argument' => 'not the default value!']);
于 2009-02-23T21:13:22.077 に答える
4

これを見つけました、これはおそらくまだ正しいです:

http://www.webmasterworld.com/php/3758313.htm

簡単な答え:いいえ。

長い答え:はい、上記で概説されているさまざまな厄介な方法で。

于 2009-02-23T21:04:02.597 に答える
0

あなたはほとんど答えを見つけましたが、アカデミック/ハイレベルのアプローチは関数カリー化であり、私は正直なところあまり用途を見つけられませんでしたが、存在することを知るのに役立ちます。

于 2009-02-23T21:07:40.267 に答える
0

ceejayozが提案するようにすべての引数を配列として渡すか、func_get_args() を解析してデフォルトのリストとマージする複雑すぎるコードを使用して、いくつかの癖を使用できます。コピーアンドペーストではなく、オブジェクトと特性を使用する必要があります。最後に、すべての種類の値を渡すことができるようにするには (null または false を除外して、デフォルトのパラメーター置換のシグナルにすることはありません)、ダミーの特殊な型 DefaultParam を宣言する必要があります。もう 1 つの欠点は、任意の IDE でタイプ ヒントまたはヘルプを取得する場合、関数宣言で名前と既定値を複製する必要があることです。

class DefaultParam {}

trait multi_arg_functions
{
  private static function multi_arg($defaults, $list, $preserve_index = false)
  {
    $arg_keys = array_slice(array_keys($defaults), 0, count($list));

    if ($preserve_index) {
      $listed_arguments = array_slice($list, 0, count($arg_keys));
      $extras = array_slice($list, count($arg_keys), null, true);
    } else {
      $listed_arguments = array_splice($list, 0, count($arg_keys));
      $extras = &$list;
    }
    unset($list);

    $arguments = array_combine($arg_keys, $listed_arguments);
    $arguments = array_filter($arguments, function ($entry) {
      return !($entry instanceof DefaultParam); //remove entries that mean default, a special class in this case
    });
    $arguments = array_merge($defaults, $arguments);

    return [$arguments, $extras];
  }
}

class b {
  use multi_arg_functions;

  static function func1($an_argument = 'a value', $another_argument = 'another value', $third_argument = 'yet another value') { //give defaults here to get hints in an IDE
      list($args, $extras) = self::multi_arg( //note: duplicate names and defaults
          [
          'an_argument' => 'a value',
          'another_argument' => 'another value',
          'third_argument' => 'yet another value!',
          ], func_get_args());

      echo json_encode(['args' => $args, 'extras' => $extras])."\n";
  }
}

$default_param = new DefaultParam();

b::func1('value 1');
b::func1('value 2', $default_param, 'third argument');
b::func1('value 3', $default_param, 'third argument', 'fourth argument');

注: preserve_index = true を使用すると、元のインデックスから開始する追加の引数を取得できます。

于 2015-11-17T21:24:22.207 に答える