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




print function arguments in php (6)

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.

I just have found out that there is a function called func_get_arg in PHP which enables developer to use variant style of getting arguments. It seems to be very useful because number of argument can now be arbitrary, but I cannot think of any good example of using it.

What are the some examples of using this function to fully benefit its polymorphic characteristic?


I hardly ever use func_get_arg(), but I do use its cousin func_get_args() quite a bit. Here's one example, a function along the lines of the echo statement that entity encodes all its arguments:

function ee() {
    $args = func_get_args();
    echo implode('', array_map('htmlentities', $args));
}

I use that function quite a bit.

Here's another useful example, a function that does the same job as SQL's COALESCE() function.

function coalesce() {
    $args = func_get_args();
    foreach ($args as $arg) {
        if (!is_null($arg)) {
            return $arg;
        }
    }
    return null;
}

It returns the first non-null argument passed in, or null if there's no such argument.


As of php5.6, there isn't a use case of func_get_arg anymore; which isn't to say that its functionality isn't needed anymore, but is 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.


Read the official document and found that func_get_args():
Gets an array of the function's argument list.

This function may be used in conjunction with func_get_arg() and func_num_args() to allow user-defined functions to accept variable-length argument lists.

<?php
        function foo()
        {
            $numargs = func_num_args();
            echo "Number of arguments: $numargs \n";
            if ($numargs >= 2) {
                echo "Second argument is: " . func_get_arg(1) . "\n";
            }
            $arg_list = func_get_args();
            for ($i = 0; $i < $numargs; $i++) {
                echo "Argument $i is: " . $arg_list[$i] . "\n";
            }
        }

        foo(1, 2, 3);
?>

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);

Personally, I don't think there is a good use case for it inside a normal function. As a control freak I like to know exactly what is being passed to my functions and I like to know exactly what I'm passing.

However, it can be use full for things like Dynamic/Static URL routing. When you are rewriting (via mod_rewrite) the URL args to a single bootstrap.

In this sense, you can have arguments that don't necessarily need to exist with every page request.







php