Jump to content

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


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;
}
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.