What is difference between Method overriding and overloading in PHP?

devquora
devquora

Posted On: Feb 22, 2018

 

Overriding and Overloading both are oops concepts.

In Overriding, a method of the parent class is defined in the child or derived class with the same name and parameters. Overriding comes in inheritance.

An example of Overriding in PHP.

<?php

class A {
   function showName() {
      return "Ajay";
   }
}

class B extends A {
   function showName() {
      return "Anil";
   }
}

$foo = new A;
$bar = new B;
echo($foo->myFoo()); //"Ajay"
echo($bar->myFoo()); //"Anil"
?>

In Overloading, there are multiple methods with the same name with different signatures or parameters. Overloading is done within the same class. Overloading is also called early binding or compile time polymorphism or static binding.

An example of Overloading in PHP

<?php 
class A{

function sum($a,$b){
	return $a+$b;

}

function sum($a,$b,$c){
	return $a+$b+$c;

}

}

$obj= new A;
$obj->sum(1,3); // 4
$obj->sum(1,3,5); // 9
?>

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Appinventiv Php Developer Interview Questions

    How to find datatype of variable in PHP?

    You can find the datatype of a variable in PHP by using the Var_dump() and gettype() functions....

    Appinventiv Php Developer Interview Questions

    What is Interface? Why it is used?

    An interface can be defined as a platform that permits users to develop programs that mention all public methods that need to be implemented by various classes. It does not involve any complexity or d...