Jump to content

Is it ok for a function to have many argument, for e.g. 5 ? e.g. below


jd2007

Recommended Posts

// example function
function ($a, $b, $c, $d, $e)
{

}

 

will this increase its speed to load ?

 

-----------

 

another question, do i need to create a function when assigning variables, e.g. below

 

which one is quicker ? the example 1 or 2...

 

example 1

 

class abc
{
protected $x;
protected $y;

function helloa($x, $y)
{
   $this->x = $x;
   $this->y = $y;
   echo $x."&".$y;
}

  function hellob($x, $y)
{
   $this->x = $x;
   $this->y = $y;
   echo $x."%".$y;
}
}

 

example2

 

class abc
{
protected $x;
protected $y;

function assign()
{
$this->x = $x;
   $this->y = $y;
}

function helloa($x, $y)
{
   $this->assign();
   echo $x."&".$y;
}

  function hellob($x, $y)
{
   $this->assign();
   echo $x."%".$y;
}
}

Don't worry about speed as it relates to number of arguments.

 

I think about five/six individual arguments should be the limit, more than that leads to possible errors of what goes where.

 

With large number of values that need to be passed to functions/methods, use objects or arrays.

another question, do i need to create a function when assigning variables, e.g. below

 

which one is quicker ? the example 1 or 2...

 

example 1

 

class abc
{
protected $x;
protected $y;

function helloa($x, $y)
{
   $this->x = $x;
   $this->y = $y;
   echo $x."&".$y;
}

  function hellob($x, $y)
{
   $this->x = $x;
   $this->y = $y;
   echo $x."%".$y;
}
}

 

 

example2

 

class abc
{
protected $x;
protected $y;

function assign()
{
$this->x = $x;
   $this->y = $y;
}

function helloa($x, $y)
{
   $this->assign();
   echo $x."&".$y;
}

  function hellob($x, $y)
{
   $this->assign();
   echo $x."%".$y;
}
}

sorry, e.g 2 was a mistake

 

class abc
{
protected $x;
protected $y;

function assign($var1, $var2)
{
$this->x = $var1;
   $this->y = $var2;
}

function helloa($x, $y)
{
   $this->assign($x, $y);
   echo $x."&".$y;
}

  function hellob($x, $y)
{
   $this->assign($x, $y);
   echo $x."%".$y;
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.