Home >> PHP >> What is overloading in PHP

What is overloading in PHP

Overloading:-When we have more than one function with same name with different arguments is called overloading.

Example:-

class shape{
      public function getArea($radius){
             return $radius*$radius*3.14;
      }
      public function getArea($width,$height){
             return $width*$height;
      }
}
$circle=new shape();
echo $circle->getArea(3);
$rectangle=new shape();
echo $rectangle->getArea(3,3);
Output:-
28.27
9

Example:-
class shape{
      const PI;
      function __call($name,$arg){
             if($name=="area"){
                   switch(count($arg)){
                          case 0:return 0;
                          case 1:return self::PI*$arg[0];
                          case 2:return $arg[0]*$arg[1];
                   }
             }
      }
}
$circle=new shape();
echo $circle->area(5);
$rectangle=new shape();
echo $rectangle->area(5,5);

Post Your Comment

Next Questions
What is overriding
What is abstraction
What is abstract class
Implementation of abstract method
What is interface
What is difference between abstract class and interface
What is inheritance
Inheritance types
What is single inheritance
What is multiple inheritance
What is multilevel inheritance
How to use static method and property in inheritance
How to use self and parent with static
How to use self and parent without static
What is visibility
What is the difference between public, private, and protected
What is final
What is final class
What is final method
What is final variable
How to print half pyramid using star
How to print half pyramid using number
How to print inverted half pyramid using star
How to print inverted half pyramid using number
How to print half pyramid using one to ten number

Copyright ©2022 coderraj.com. All Rights Reserved.