Hi all

I'm used to programming in Java, which does support method overloading.
So when starting to use PHP, I quickly found that this was not supported in PHP.

But why and how is this supported with "system methods" (I don't know if that name is correct) like, for example, mktime?
see attachment.

Or am I totally wrong and IS method overloading supported? If it is, it did not seem to work when I tried, how am I supposed to do it then?

Thx in advance.
Greets, K?!.

Recommended Answers

All 4 Replies

From what I was reading it doesn't seem that method overloading is possible in php. "However, PHP 5 provides a way to imitate overloading by catching calls to “inaccessible methods” with magic method __call."

http://www.xml.lt/Blog/2008/12/02/Method+overloading+in+PHP+5

On a side note. It is possible to use method overload in java. No reserved words are needed, all you have to do is create a method with different input variables and identical name.
ex.

public static void isOverload(int x){ }
public static void isOverload(double x) {}
public static void isOverload(String x, int y, long z) { }

First: Oops, it seems like I forgot the attachment.
I posted 2 attachments now, which will make it more clear what I mean.

Answer: uhu, indeed, the solution posted on that site is the solution I used(in a slightly different form) to be able to use method overloading.

Check the images in the attachment, the first one shows a "system method", with good overloading (no "methodname1", "methodname2"), and the second one shows the result if you use the solution from that site.

Why and how is method overloading possible with these php predefined methods, and not with methods from self-made classes?

Look into func_get_args() . Kind of what you are looking for.

Yes, I'm already using func_get_args(), but I was wondering how the PHP developers get real method overloading (or at least something that looks like real method overloading), while the solution I use is only is something that comes near to it.

With the real overloading, you can just select the function you need, and you get all thi input parameters as a list (of course you still have to change them, but you get to see what has to come in which position. (in netbeans or other IDE's, won't work in a text editor of course ;))

With the mentioned approach, you need to select, "methodname()" (no arguments), and then that method, according to the parameters you put in, selects "methodname1($param1)", "methodname2($param1, $param2)", ...

It's not like I really need it or something, I just wanted to know why it's not possible for us while the PHP developers succeed to do it (or at least it really looks like it).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.