method overloading in a class

From: Date: Wed, 18 Aug 2010 16:23:47 +0000
Subject: method overloading in a class
Groups: php.general 
Request: Send a blank email to php-general+get-307535@lists.php.net to get a copy of this message
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

« previous php.general (#307535) next »