func_num_args() and func_get_arg()

December 13, 2007 – 2:29 am

func_num_args() and func_get_arg() are both very useful and powerful function that can be called from user defined functions.
Now these examples are taken from the php.net


<?
function foo()
{
    $numargs = func_num_args();
    echo "Number of arguments: $numargs";
}
foo(1, 2, 3);    // Prints 'Number of arguments: 3'
?>

The example should be pretty intuitive but if not, let me explain. func_num_args() returns the number of variable number of arguments that were passed to a function. Like the in he above example three arguments were passed to function foo.


<?
function foo()
{
     $numargs = func_num_args();
     echo "Number of arguments: $numargs";
     if ($numargs = 2) {
         echo "Second argument is: " . func_get_arg(1) . "n";
     }
}
foo (1, 2, 3);
?>

In this example func_get_arg gets a copy of the “ith” variable number of argument.
Now if you combine these two functions, you can create something like this.


<?
function append_default(&$obj)
{
	$default = func_get_arg(1);
	for($i = 2, $total = func_num_args(); $i < $total; $i++)
		$obj->{func_get_arg($i)} = $default;
}
append_default($some_obj, "0", "age", "height", "weight");
?>

In this example, you can call function on some object and have default values appended to its member variables. In this example the $some_obj will have its age, height, weight, member variables get a default value of “0″. This works because func_get_arg(1) will return a copy of the first variable number of argument. This is important to not because you can’t pass a reference to an object to as variable like above. This is why the append_default explicitly requires a reference to an object. Also notice a good php performance speed up technique in the above code. It is inefficient to have to recompute func_num_args at every loop. Rather it is better practice, to save the result of the function like in the above code.

  1. 5 Responses to “func_num_args() and func_get_arg()”

  2. Great stuff, we want some non techno stuff to be written too!
    and welcome to the blogging world.

    By Kag on Dec 13, 2007

  3. you’re missing a greater than in your last example

    By Matt Kho on Feb 28, 2008

  4. Yeah thanks for catching that. Wordpress makes it hard to edit code since it converts > to & gt ; after you save it. Then when you edit it you won’t convert & gt; back to >.

    It treats lines break in a more stupid matter. The first time it will add it as < br / >, second it will look like & lt; br / & gt; , lol.

    By Jesus DeLaTorre on Feb 28, 2008

  5. func_get_arg() does not appear to be allowed to be used as a function argument itself within class constructors in PHP 502 (wonk-ay!!!):

    class DEF extends ABC { function __construct() { parent::__construct(func_get_arg(0), func_get_arg(1), func_get_arg(2));

    func_get_arg is useful, if you know the exact order of the arguments, or if the order doesn’t matter i use this function (or func_get_args) for example to create own sprintf wrappers

    By Starly Jackson on Dec 23, 2009

  6. @Starly If you think about it, it makes sense. The scope of func_get_arg should only be within a function. Which function would func_get_arc be referring to when it is passed as an argument to a function?

    By Jesus DeLaTorre on Dec 25, 2009

Post a Comment