func_num_args(), func_get_args() and func_get_arg() can be very useful to emulate overloading in PHP.
Suppose you have a class to add a user in a system, and that you want to allow 2 ways of doing it. The first way would be passing an array with all the user info in it, and the second way would be passing each user attribute as a single argument.
<?php
class Test {
function insertUser() {
// gets the number of parameters
$numArgs = func_num_args();
// make decisions based on the arguments number
if ($numArgs == 1) {
// if it's only one argument, we suppose that it is an array with user info
// gets the first argument
$user = func_get_arg(0);
// checks if it really is an array
if (is_array($user)) {
// here you should check if the array contains all necessary fields
// adds the user
echo "User added.<br/>";
echo "ID: " . $user["id"] . "<br/>";
echo "NAME: " . $user["name"] . "<br/>";
echo "EMAIL: " . $user["email"] . "<br/>";
} else {
// generates an error if argument is not an array
echo "Argument is not an array: " . $user . ".<br/>";
}
} else if ($numArgs == 3) {
// if the function receives 3 arguments, we assume that they
// are 'id', 'name' and 'email' respectively
// inserts the user into the system
echo "User added.<br/>";
echo "ID: " . func_get_arg(0) . "<br/>";
echo "NAME: " . func_get_arg(1) . "<br/>";
echo "EMAIL: " . func_get_arg(2) . "<br/>";
} else {
// if the number of arguments is different from 1 and 3
// an error will be generated
echo "Wrong argument number.<br/>";
echo "Arguments received: " . func_num_args();
}
}
}
// creates an Test object
$objTest = new Teste();
// inserts an user passing an array with all his info
$objTest->insertUser(array("id" => 1, "name" => "George W. Bush", "email" => "jackass@whitehouse.gov"));
echo "<br/>";
// inserts an user providing each attribute as a single argument
$objTest->insertUser(2, "Vicente Fox", "iloveusa@disney.com");
echo "<br/>";
// this will generate an error, because only 2 arguments were passed
$objTest->insertUser(3, "Tony Blair");
?>
func_num_args
(PHP 4, PHP 5)
func_num_args — Returns the number of arguments passed to the function
Description
Gets the number of arguments passed to the function.
This function may be used in conjunction with func_get_arg() and func_get_args() to allow user-defined functions to accept variable-length argument lists.
Return Values
Returns the number of arguments passed into the current user-defined function.
ChangeLog
| Version | Description |
|---|---|
| 5.3.0 | This function can now be used in parameter lists. |
Errors/Exceptions
Generates a warning if called from outside of a user-defined function.
Examples
Example #1 func_num_args() example
<?php
function foo()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs\n";
}
foo(1, 2, 3); // Prints 'Number of arguments: 3'
?>
Notes
Note: Because this function depends on the current scope to determine parameter details, it cannot be used as a function parameter. If this value must be passed, the results should be assigned to a variable, and that variable should be passed.
func_num_args
20-Jan-2007 03:18
11-May-2006 03:40
Just adding a note to JARED's Note.
func_num_args() works if and only if called from within a function definition BECAUSE it calculates the number of arguments sent while function calling. That is why it gives
'1' in helloWorld("HelloWorld!") and '2' in helloWorld("HelloWorld!", "HowdyWorld!") - There is nothing to do with DEFAULT args.
If you call the same function as helloWorld(); "No args passed"
It will give a warning for wrong function call ("Warning: Missing argument 1 for helloWorld()") and the result of func_num_args() will be '0'.
16-Mar-2006 03:24
This function comes in extremely handy in OO Classes. My focus is currently PHP 4, but this technique probably works just as well in PHP 5.
<?php
Class foo {
// constructor, other methods, etc
function somedata($somedata = '') {
if (func_num_args() > 0) {
$this->somedata = $somedata;
}
return $this->somedata;
}
}
?>
This allows for a default value to be set, while not requiring the parameter. (Yes, I'm aware that it only warns when you do, but I think this is a little cleaner)
06-Sep-2005 12:46
Just a note for anyone wondering. This function doesn't include params that have a default value, unless you pass one in to overwrite the default param value. Not sure if that makes sense, so here's an example:
<?php
function helloWorld($ArgA, $ArgB="HelloWorld!") {
return func_num_args();
}
// The following will return 1
$Returns1 = helloWorld("HelloWorld!");
// The following will return 2
$Returns2 = helloWorld("HelloWorld!", "HowdyWorld!");
?>
11-May-2005 10:16
func_num_args() can be used in conjunction with named arguments, also. For example:
<?php
function DebugShow( $label, $value ) {
echo "# " . $label ;
if ( func_num_args() > 1 ) echo " = " . $value ;
echo "<br>";
}
?>
17-Apr-2002 07:40
If you want to pass the parameters on intact to another function, use func_get_args and call_user_func_array (careful - this one is only available in recent PHP versions). For example:
<?php
/* Print an HTML tag. This accepts a variable number of arguments:
the first should be the name of the tag, followed by pairs of
arguments that describe keys and values. The values are printed
with surrounding double quote characters. */
function printTag() {
$numArgs = func_num_args();
if ($numArgs < 1) die("printTag given no arguments");
echo "<" . func_get_arg(0);
for ($i = 1; $i < $numArgs; $i+=2) {
echo " " . func_get_arg($i);
if ($i+1 < $numArgs)
echo "=\"" . func_get_arg($i+1) . "\"";
}
echo ">";
}
/* Print an HTML tag with a newline on the end */
function printTagNL() {
$args = func_get_args();
call_user_func_array("printTag", $args);
echo "\n";
}
printTagNL("input", "type", "hidden", "name", "SORTORDER", "value", $columnNo);
?>
29-Mar-2002 04:05
The idea of func_get_args() is to construct functions of variable number of parameters like
<?php
function var_param_func(){
if(func_num_args()==0){
//do one thing
}
if(func_num_args()==1)
//do another thing
//get the args with func_get_args()
}
}
?>
