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 us
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
.
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);
?>
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)
.
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.
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.
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);