473,394 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

method overloading.

I'm trying to find the best way to do method overloading in the least
hackish way.

First solution:
Using __call() and a switch (or two)

class MyClass {
public __call( $method, $args ) {
//maybe some code here to determine arg count and arg types with
gettype()
switch( $method ) {
case "foo":
//Another switch in here using arg count and types
break;
case "bar":
//Another switch in here using arg count and types
break;
case default:
//Throw an exception
break;
}
}

$obj = new MyClass();
$obj->foo();
$obj->foo("str");
.....
Second solution: Using default types

class MyClass {
public function foo( $arg1=$something, $arg2=$another ) {
// Do something
}
}

$obj = new MyClass();
$obj->foo(); // Fine because we have default args
$obj->foo( 1, 2 ); // Fine we are using different values
$obj->foo( null, 2 ); // Fine but passing null is ugly.. I don't like
this.

What I was hoping for in the second solution would be explicitly pass
arg values ( Python-like ):

$obj->foo( $arg2 = 2 ); //This case we are using the default value
for arg1 but arg2 is overwritten.
I understand ( or so I think ) with in PHP would resolve to:
$obj->foo( 2 ); // Since $arg = 2 is evaluated then passed at the
first argument (arg1) arg2 is set to default

This may be a pipe dream but is there any way to do explicit passing
like Python in PHP? I can't seem to find anything in the docs so i'm
assuming there isn't.
Jun 27 '08 #1
2 1244
..oO(Derek Schrock)
>I'm trying to find the best way to do method overloading in the least
hackish way.

First solution:
Using __call() and a switch (or two)
[...]

Second solution: Using default types

class MyClass {
public function foo( $arg1=$something, $arg2=$another ) {
// Do something
}
}

$obj = new MyClass();
$obj->foo(); // Fine because we have default args
$obj->foo( 1, 2 ); // Fine we are using different values
$obj->foo( null, 2 ); // Fine but passing null is ugly.. I don't like
this.
You could also play with a variable number of arguments, see
func_get_arg() etc.
>What I was hoping for in the second solution would be explicitly pass
arg values ( Python-like ):

$obj->foo( $arg2 = 2 ); //This case we are using the default value
for arg1 but arg2 is overwritten.
I understand ( or so I think ) with in PHP would resolve to:
$obj->foo( 2 ); // Since $arg = 2 is evaluated then passed at the
first argument (arg1) arg2 is set to default

This may be a pipe dream but is there any way to do explicit passing
like Python in PHP? I can't seem to find anything in the docs so i'm
assuming there isn't.
Nope. Skipping arguments like that is not allowed in PHP.

Another option (still ugly, though) might be to pass some or all
arguments as an associative array:

public function foo(array $args) {
...
}

And then:

$obj->foo(array('arg1' =23, 'arg2' =42));

Micha
Jun 27 '08 #2
On Jun 12, 1:11*pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Derek Schrock)
I'm trying to find the best way to do method overloading in the least
hackish way.
First solution:
Using __call() and a switch (or two)
[...]
Second solution: Using default types
class MyClass {
*public function foo( $arg1=$something, $arg2=$another ) {
* * // Do something
*}
}
$obj = new MyClass();
$obj->foo(); *// Fine because we have default args
$obj->foo( 1, 2 ); // Fine we are using different values
$obj->foo( null, 2 ); // Fine but passing null is ugly.. I don't like
this.

You could also play with a variable number of arguments, see
func_get_arg() etc.
What I was hoping for in the second solution would be explicitly pass
arg values ( Python-like ):
$obj->foo( $arg2 = 2 ); *//This case we are using the default value
for arg1 but arg2 is overwritten.
I understand ( or so I think ) with in PHP would resolve to:
$obj->foo( 2 ); // Since $arg = 2 is evaluated then passed at the
first argument (arg1) arg2 is set to default
This may be a pipe dream but is there any way to do explicit passing
like Python in PHP? I can't seem to find anything in the docs so i'm
assuming there isn't.

Nope. Skipping arguments like that is not allowed in PHP.

Another option (still ugly, though) might be to pass some or all
arguments as an associative array:

* public function foo(array $args) {
* * ...
* }

And then:

* $obj->foo(array('arg1' =23, 'arg2' =42));

Micha
Yeah, that was my third option.
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John Smith | last post by:
can we overload a javascript function with different argument? example: function a(a){} function a(a,b){}
18
by: Daniel Gustafsson | last post by:
Hi there, hmm, I've just started with C# and I'm experimenting with method overloading. It seems like it's not possible to override method using return types, only parameters. Is that by design,...
9
by: Ryan Taylor | last post by:
Hello. I am trying to overload a method so that I have four possible working copies. The only difference between the four methods is what to search by and in what manner to return the results....
2
by: Iter | last post by:
Hi Guys, In my company, we have java application which is runing in unix platform, and we have web service application build by microsoft .net runing in IIS in microsoft windows 2000 platform....
1
by: Ratnakar .N | last post by:
HELLO, Please tell me the main difference between method overriding and method overloading Thank you
10
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. ...
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
5
by: Andreas Schmitt | last post by:
I have a problem here. I've read a book about C# already and got the basics of how the language handles polymorphism I think but I ran into a problem here that I simply never even thought of as a...
1
by: Zach | last post by:
Consider the following code: void Test(int i) { System.Console.WriteLine("int function"); } void Test(object o) { System.Console.WriteLine("object function");
10
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.