In languages like Java, overloading can be used in this way:

void test($foo, $bar){}
int test($foo){}

Then if you called test() with 2 arguments e.g test($x, $y);, the first function would be called. If you passed only 1 argument e.g test($x);, the 2nd function would be called.

From the manual it seems that php 5 does have overloading, but what is it for? I can't seem to understand the manual on this topic..

+13  A: 

PHP's meaning of overloading is different than Java's. In PHP, overloading means that you are able to add object members at runtime, by implementing some of the __magic methods, like __get, __set, __call, __callStatic. You load objects with new members.

Overloading in PHP provides means to dynamically "create" properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types.

Some example:

class Foo
    public function __call($method, $args)
        echo "Called method $method";

$foo = new Foo;

$foo->bar(); // Called method bar
$foo->baz(); // Called method baz

And by the way, PHP supports this kind of overloading since PHP 4.3.0. The only difference is that in versions prior to PHP 5 you had to explicitly activate overloading using the overload() function.

Ionuț G. Stan
What can be any possible advantages of that type of overloading whose example you gave???
Click Upvote
One scenario in which `__call()` proved to be very useful was when I built a currency converter class. Instead of calling `$converter->convert(2, 'EUR', 'USD');` you could actually call `$converter->convertEurToUsd(2);`, which is much more readable in my opinion. You may take a look at my sources: http://github.com/igstan/php-utils/blob/master/tests/CurrencyConverterTest.php http://github.com/igstan/php-utils/blob/master/src/CurrencyConverter.php
Ionuț G. Stan
I use this method for a dynamic LINQ-like interface to my database. So I can do things like $db->Select('table')->col_1[$value]->col_2[$value]->Merge('col_1'); So all of the __calls(). Get passed through to the correct handling class (i.e. SQL, File, Xml).
Many PHP ORM libraries use the __get and __set methods to do what null is talking about.
Your currency converter code is very neatly written and formatted! What formatting guidelines do you follow when you write your code?
Click Upvote
Thanks! Really appreciated. I mainly use the PEAR/Zend Framework coding conventions. I keep lines no longer than 79 chars, methods as short as possible and I line up variable declarations in columns. Anyway, GitHub's syntax highlighter has its merits too :)
Ionuț G. Stan
+3  A: 

If you want to overload a function like in Java, don’t specify any arguments and use the func_num_args and func_get_args function to get the number of arguments or the arguments themselves that were passed to that function:

function test() {
    $args = function_get_args();
    swtich (count($args)) {
        case 1:
            // one argument passed
        case 2:
            // two arguments passed
            // illegal numer of arguments