Login |  Register 
I've learnt so much after subscribing to read your exclusive articles!
Nick
 

Function parameters

In PHP you are allowed to pass parameters to functions which usually configures the function altering the way it will respond.

Function default parameters

However there are several options on how function parameters are handled in PHP. The first is to set a default for a parameter in the function declaration which defines a default value.

In PHP there isn't the option to perform function overloading however this can to some extent be used to the same effect.

//An example of Method overloading in C#
public void AddUser(string username)
{
 AddUser(string username, GeneratePassword());
}

public void AddUser(string username, string password)
{
 AddUser(string username, string password, "Default", "User");
}

public void AddUser(string username, string password, string firstname, string lastname)
{
 /*
  *
 */
}
//An example of parameter defaults in PHP

public function AddUser($username, $password, $firstname = "Default", $lastname = "User")
{
 /*
  *
 */
}

As you can see I was unable to add as many defaults to the method as was done in C#. This is because when a default parameter is defined only constant values and so not variables or functions are allowed to be used.

function AddRole($role = 'default', $user_id)
{
 /*
  *
 */
}

AddRole(12);

Therefore when the above function is called the role isn't specified just the $user_id unfortunatley this will cause an error as the 12 will be passed to the $role parameter leaving the $user_id unspecified.

Furthermore function parameters with a default specified should always be at the end of the function parameter list. Furthermore if you want to specify a parameter within several defaulted parameters the parameters leading up to the specified parameter must be provieded as well. As can be seen with the parameter $role_type

function AddRoleDetails($role = 'default', $user_id, $description='Default details', $role_type='Normal', $role_admin="Default")
{
 /*
  *
 */
}

AddRoleDetails('default', 12, 'Default details', 'High');

Functions with a variable number of parameters

Functions can be passed in PHP a variable number of parameters

public function AddTemplate()
{
 $args = func_get_args();
}

The function func_get_args() all the parameters passed to a function to be returned as an array. Sometimes it is you expect a minimum number of parameters to be passed to the function and func_num_args() can be used to ensure there is the right number of parameters.

You can mix and match variable length argument lists with set argument lists so...

public function AddTemplate($templateName)
{
 $params = func_get_args();
}

Unfortunatley this will mean that you can access the variable $templateName directly and through $params. It is however better to remove $templateName from the parameter list. Like so...

public function AddTemplate($templateName)
{
 $params = func_get_args();
 unset($params[0]);
}

Note that defined parameters are in the argument list array in order. That is why the value for $templateName is first before any other variable in the array.

public function AddTemplate()
{
 if (func_num_args() < 1)
 {
  throw new Exception("You need to add more parameters to this function!");
 }
 $args = func_get_args();
}

You can also get a function parameter at a particular position in the argument list by using func_get_arg(). Simply pass a integer indicating the index position of the parameter to be used to the function and it will return only that value.

AddPeople("Chris", "Sarah", "Jane");
function AddPeople()
{
 echo func_get_arg(1);
}

So the above example will return "Sarah".


No comments have been provided.
security image
Written by Dominic Skinner
Last Updated: 2011-10-25 16:00:38