[PHP] method overloading in a class

2010-08-18 Thread Ashley Sheridan
Hi list,

I know that some languages such as C++ can overload functions and
methods by declaring the method again with a different number of
arguments, and the compiler internally sorts things out, but I can't
seem to find a similar way to do this with PHP.

Basically, what I've got at the moment is a class method with 2
arguments, and I need to be able to overload the method with 3
arguments. The following which would work in other languages doesn't
seem to bring any joy in PHP:

class foo
{
public function bar($arg1, $arg2)
   {
// do something with $arg1  $arg2
}

public function bar($arg1, $arg2, $arg3)
{
// do something different with all 3 args
}
}

Is there any feasible way of doing this? The method names really need to
remain the same as they exist as part of a framework, but the arguments
really server quite different purposes between the two methods, so
there's no nice way of just merging the two functions without breaking
the naming conventions, etc used.

Thanks,
Ash
http://www.ashleysheridan.co.uk




Re: [PHP] method overloading in a class

2010-08-18 Thread chris h
Would something like this work for you?

class foo
{

   public function bar($arg1, $arg2, $arg3=null)
  {

 if (isset($arg3)){
 {
return $this-_bar3($arg1, $arg2, $arg3);

 } else {
return $this-_bar2($arg1, $arg2);

 }

  }


also you may want to look into the func_get_args function.



Chris.


On Wed, Aug 18, 2010 at 12:23 PM, Ashley Sheridan
a...@ashleysheridan.co.ukwrote:

 Hi list,

 I know that some languages such as C++ can overload functions and
 methods by declaring the method again with a different number of
 arguments, and the compiler internally sorts things out, but I can't
 seem to find a similar way to do this with PHP.

 Basically, what I've got at the moment is a class method with 2
 arguments, and I need to be able to overload the method with 3
 arguments. The following which would work in other languages doesn't
 seem to bring any joy in PHP:

 class foo
 {
public function bar($arg1, $arg2)
   {
// do something with $arg1  $arg2
}

public function bar($arg1, $arg2, $arg3)
{
// do something different with all 3 args
}
 }

 Is there any feasible way of doing this? The method names really need to
 remain the same as they exist as part of a framework, but the arguments
 really server quite different purposes between the two methods, so
 there's no nice way of just merging the two functions without breaking
 the naming conventions, etc used.

 Thanks,
 Ash
 http://www.ashleysheridan.co.uk





Re: [PHP] method overloading in a class

2010-08-18 Thread Ashley Sheridan
On Wed, 2010-08-18 at 12:35 -0400, chris h wrote:
 
 
 Would something like this work for you?
 
 
 class foo
 {
 
 
public function bar($arg1, $arg2, $arg3=null)
   {
 
 
  if (isset($arg3)){
  {
 return $this-_bar3($arg1, $arg2, $arg3);
 
 
  } else {
 return $this-_bar2($arg1, $arg2);
 
 
  }
 
 
 
   }
 
 
 
 
 also you may want to look into the func_get_args function.
 
 
 
 
 
 
 
 Chris.
 
 
 
 
 
 
 On Wed, Aug 18, 2010 at 12:23 PM, Ashley Sheridan
 a...@ashleysheridan.co.uk wrote:
 
 Hi list,
 
 I know that some languages such as C++ can overload functions
 and
 methods by declaring the method again with a different number
 of
 arguments, and the compiler internally sorts things out, but I
 can't
 seem to find a similar way to do this with PHP.
 
 Basically, what I've got at the moment is a class method with
 2
 arguments, and I need to be able to overload the method with 3
 arguments. The following which would work in other languages
 doesn't
 seem to bring any joy in PHP:
 
 class foo
 {
public function bar($arg1, $arg2)
   {
// do something with $arg1  $arg2
}
 
public function bar($arg1, $arg2, $arg3)
{
// do something different with all 3 args
}
 }
 
 Is there any feasible way of doing this? The method names
 really need to
 remain the same as they exist as part of a framework, but the
 arguments
 really server quite different purposes between the two
 methods, so
 there's no nice way of just merging the two functions without
 breaking
 the naming conventions, etc used.
 
 Thanks,
 Ash
 http://www.ashleysheridan.co.uk
 
 
 
 

Thanks to everyone, I decided to modify the existing original method and
use func_get_args() to grab the arguments passed to it. It's not
perfect, because I lose out on the automatic value assignment that I
would have with a regular method (i.e. function foo($bar, $fubar=0) etc)
but it will do at a pinch.

It's a shame this sort of overloading isn't supported, as it could be
quite a useful feature, but it's not a complete show-stopper.

Thanks,
Ash
http://www.ashleysheridan.co.uk




Re: [PHP] method overloading in a class

2010-08-18 Thread Adam Richardson
On Wed, Aug 18, 2010 at 12:23 PM, Ashley Sheridan
a...@ashleysheridan.co.ukwrote:

 Hi list,

 I know that some languages such as C++ can overload functions and
 methods by declaring the method again with a different number of
 arguments, and the compiler internally sorts things out, but I can't
 seem to find a similar way to do this with PHP.

 Basically, what I've got at the moment is a class method with 2
 arguments, and I need to be able to overload the method with 3
 arguments. The following which would work in other languages doesn't
 seem to bring any joy in PHP:

 class foo
 {
public function bar($arg1, $arg2)
   {
// do something with $arg1  $arg2
}

public function bar($arg1, $arg2, $arg3)
{
// do something different with all 3 args
}
 }

 Is there any feasible way of doing this? The method names really need to
 remain the same as they exist as part of a framework, but the arguments
 really server quite different purposes between the two methods, so
 there's no nice way of just merging the two functions without breaking
 the naming conventions, etc used.

 Thanks,
 Ash
 http://www.ashleysheridan.co.uk



Hi Ashley,

Sorry for a slow reply, but I've found a free second and I'd like to toss
out the scheme I've used most often.

If args 1 and 2 are consistent in terms of type and usage across the two
methods, I simply add arg3 with a default to null and conditionally call a
private method that performs the special operation (documentation and
primary entry point remain in one place.  Essentially, I just add a guard
clause to the original function.  The

class foo
{
   public function bar($arg1, $arg2, $arg3 = null)
  {
   if (!is_null($arg3)) return _specialbar($arg1, $arg2, $arg3);

   // do something with $arg1  $arg2
   }

   public function _specialbar($arg1, $arg2, $arg3)
   {
   // do something different with all 3 args
   }
}

If, however, the functions differ significantly in terms of signature, I
tend to write a wrapper function that chooses the appropriate internal call
(e.g., newbar()), but this doesn't play too nicely with documentation within
the PHP ecosystem (although within other language systems such as Java, C#,
and Erlang, it's really quite nice and elegant), so I tend to structure my
code to make use of option one when possible.

Adam

-- 
Nephtali:  PHP web framework that functions beautifully
http://nephtaliproject.com