19

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..

Ali
  • 261,656
  • 265
  • 575
  • 769
  • See also [Why PHP doesn't support function overloading?](http://programmers.stackexchange.com/a/190550/84349). – Peter Krauss Mar 18 '13 at 14:33
  • You can use overloadable class in this link: http://stackoverflow.com/questions/4697705/php-function-overloading/27231803#27231803 – Hisham Dalal Dec 01 '14 at 15:55

3 Answers3

26

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.

An 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
  • 176,118
  • 18
  • 189
  • 202
  • What can be any possible advantages of that type of overloading whose example you gave??? – Ali Oct 02 '09 at 23:49
  • 8
    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 Oct 02 '09 at 23:52
  • 2
    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). – null Oct 03 '09 at 00:24
  • Many PHP ORM libraries use the __get and __set methods to do what null is talking about. – davidtbernal Oct 03 '09 at 00:34
  • Your currency converter code is very neatly written and formatted! What formatting guidelines do you follow when you write your code? – Ali Oct 03 '09 at 00:53
  • 1
    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 Oct 03 '09 at 01:35
  • That's a clever little solution there, Ionut. – abeger Dec 20 '10 at 16:39
15

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 = func_get_args();
    switch (count($args)) {
        case 1:
            // one argument passed
            break;
        case 2:
            // two arguments passed
            break;
        default:
            // illegal numer of arguments
            break;
    }
}
sradforth
  • 2,176
  • 2
  • 23
  • 37
Gumbo
  • 643,351
  • 109
  • 780
  • 844
-1

To over load a function simply do pass parameter as null by default,

class ParentClass
{
   function mymethod($arg1 = null, $arg2 = null, $arg3 = null)  
   {  
     if( $arg1 == null && $arg2 == null && $arg3 == null ){ 
        return 'function has got zero parameters <br />';
     }
     else{
       $str = '';
       if( $arg1 != null ) 
        $str .= "arg1 = ".$arg1." <br />";

       if( $arg2 != null ) 
        $str .= "arg2 = ".$arg2." <br />";

       if( $arg3 != null ) 
        $str .= "arg3 = ".$arg3." <br />";

       return $str;
      }
   }
}

// and call it in order given below ...

$obj = new ParentClass;

echo '<br />$obj->mymethod()<br />';
echo $obj->mymethod();

echo '<br />$obj->mymethod(null,"test") <br />';
echo $obj->mymethod(null,'test');

echo '<br /> $obj->mymethod("test","test","test")<br />';
echo $obj->mymethod('test','test','test');
Adil Abbasi
  • 3,161
  • 1
  • 40
  • 35