What is the good example of using 'func_get_arg' in PHP?

I usually use func_get_args() which is easier to use if wanting multiple arguments.

For example, to recreate PHP's max().

function max() {
   $max = -PHP_INT_MAX;
   foreach(func_get_args() as $arg) {
      if ($arg > $max) {
          $max = $arg;
      }
   }
   return $max;
}

CodePad.

Now you can do echo max(1,5,7,3) and get 7.


Let's say we have multiple arrays containing data in which we need to search across the keys for their values without merging these arrays.

The arrays are like:

$a = array('a' => 5, 'b' => 6);
$b = array('a' => 2, 'b' => 8);
$c = array('a' => 7, 'b' => 3);

In that case, say we need to get all the values of the key a from all the arrays. We can write a function that take in arbitrary number of arrays to search in.

// we need the key, and at least 1 array to search in
function simpleSearchArrays($key, $a1){
    $arrays = func_get_args();
    array_shift($arrays); // remove the first argument, which is the key
    $ret = array();
    foreach($arrays as $a){
        if(array_key_exists($key, $a)){
            $ret[] = $a[$key];
        }
    }
    return $ret;
}

So if we use the function:

 $x = simpleSearchArrays('a', $a, $b, $c);

$x will then contain array(5, 2, 7).


First of all, you are using the term "polymorphism" totally wrong. Polymorphism is a concept in object-oriented programming, and it has nothing to do with variable number of arguments in functions.

In my experience, all func_get_args allows you to do is add a little syntactic sugar.

Think of a function that can take any number of integers and return their sum. (I 'm cheating, as this already exists in array_sum. But cheating is good if it keeps the example simple). You could do it this way:

// you can leave "array" out; I have it because we should be getting one here
function sum1(array $integers) {
    return array_sum($integers);
}

Now you would call this like so:

$sum = sum1(array(1));
$sum = sum1(array(1, 2, 3, 4));

This isn't very pretty. But we can do better:

function sum2() {
    $integers = func_get_args();
    return array_sum($integers);
}

Now you can call it like this:

$sum = sum2(1);
$sum = sum2(1, 2, 3, 4);

As of php5.6, there isn't a use case of func_get_arg anymore; its functionality has been replaced by the variadic function using the ... syntax:

/**
 * @param array $arguments
 */
public function poit(...$arguments)
{
     foreach($arguments as $argument) {
         ...
     }
}

This is especially useful if there are methods that are overloaded at the end; one does need to filter out the first arguments anymore, as showcased by an example:

Old style using func_get_arg:

function foo($a, $b) {
    $c = array();
    if (func_num_args() > 2) {
        for($i = 0; $i < func_num_args()-2; $i++) {
            $c[] = func_get_arg($i+2);
        }
    }
    var_dump($a, $b, $c)
    // Do something
}
foo('a', 'b', 'c', 'd');

Newer variadic style;

function foo($a, $b, …$c) {
    var_dump($a, $b, $c);
    // Do something
}
foo('a', 'b', 'c', 'd');

Both foo produce the same output, one is much simpler to write.

Tags:

Php