Overloading is defining functions that have similar signatures, yet have different parameters. Overriding is only pertinent to derived classes, where the parent class has defined a method and the derived class wishes to override that method.

In PHP, you can only overload methods using the magic method __call.

An example of overriding:

<?php

class Foo {
   function myFoo() {
      return "Foo";
   }
}

class Bar extends Foo {
   function myFoo() {
      return "Bar";
   }
}

$foo = new Foo;
$bar = new Bar;
echo($foo->myFoo()); //"Foo"
echo($bar->myFoo()); //"Bar"
?>

Overloading in PHP

As we know that we can not implement overloading by create 2 function in with same name in class. So to implement overloading in php we will take help of magic method __call. Magic method __call invoked when method called by class object is not available in class. So here we will not create method exactly and will take help of __call method. Now call method will provide us 2 argument, 1st name of the method called and parameter of the function. Now with the help of either switch case or if else we will implement overloading in php. Following is very simple example of overloading in php.


class ABC {

    public function displayMessage($para1) {

        echo "Ffirst function displayMessage with parameter as para1";

    }

    public function displayMessage($para1,$para2) {

       	echo "Second function displayMessage with parameter as para1\2";

    }

}

$obj1 = new ABC;
$obj1->displayMessage('Hello');

if above example code convert to Java or C++, it will work without any errors. But if we run above code, it will throw error the error “Cannot redeclare ABC::displayMessage()”. Simple overloading is not supported by PHP. But you can implement overloading by using the PHP magic method __call(). Example:

class ABC { 

    public function __call($method_name,$arguments) {

        $methodArray = array('displayMessage1','displayMessage2');
        if (in_array($method_name,$methodArray) === false) {
            die("\n Method does not exist");
        } 

        if (count($arguments) === 2) {
            $this->displayMessage2($arguments[0],$arguments[1]);
        }

        elseif (count($arguments) === 1) {
            $this->displayMessage1($arguments[0]);
        }

        else {
            echo "\n unknown method";
            return false;
        }
    }

  function displayMessage1($var1) {
        echo "\n from func1($a)";
    }

  function displayMessage2($var1,$var2) {
        echo "\n from func2($var1,$var2)";
    } 

} 

$obj1 = new ABC;
$obj1->displayMessage1('hello');
$obj1->displayMessage2('hello','hello2');
$obj1->displayMessage3('Hello');

__call() function is triggered when invoking inaccessible methods in an object context. The syntax for __call() is mixed __call(string $name,array $arguments).
The $name parameter is the name of the method being called. The $arguments parameter is an enumerated array containing parameters passed to the $name method.

 

Leave a comment