Mostafa Talebi
Mostafa Talebi

Reputation: 9183

PHP how to pass a variable-length function parameters to another variable length function?

I have two functions with an optional amount of variables:

function getTexts()
{
  $this->printTextToDashboard();
}

function printTextToDashboard()
{
  $args = func_get_args();
  $argsNum = count($args);
  for($i=0; $i < $argsNum; $i++)
  {
    echo "This is the $i text: "."<br />".$args[$i]."<br />";
  }
}

For instance I want to call getTexts like this:

getTexts($name, $lastname, $email);

I know it has something to do with user_func_* , but I'm unable to implement it.

Thanks in advance

Upvotes: 1

Views: 64

Answers (1)

u_mulder
u_mulder

Reputation: 54841

Something like this should work:

function getTexts()
{
    call_user_func_array(array($this, 'printTextToDashboard'), func_get_args());
}

Upvotes: 2

Related Questions