Method overriding in php tutorialspoint


Function Overloading in PHP

Function overloading is a feature that permits making creating several methods with a similar name that works differently from one another in the type of the input parameters it accepts as arguments.

Example

Let us now see an example to implement function overloading−

 Live Demo

<?php
   class Shape {
      const PI = 3.142 ;
      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(3);
   $rect = new Shape();
   echo $rect->area(8,6);
?>

Output

This will produce the following output−

9.42648

Function Overriding in PHP

In function overriding, the parent and child classes have the same function name with and number of arguments

Example

Let us now see an example to implement function overriding−

 Live Demo

<?php
   class Base {
      function display() {
         echo "\nBase class function declared final!";
      }
      function demo() {
         echo "\nBase class function!";
      }
   }
   class Derived extends Base {
      function demo() {
         echo "\nDerived class function!";
      }
   }
   $ob = new Base;
   $ob->demo();
   $ob->display();
   $ob2 = new Derived;
   $ob2->demo();
   $ob2->display();
?>

Output

This will produce the following output−

Base class function!
Base class function declared final!
Derived class function!
Base class function declared final!

Method overriding in php tutorialspoint

Updated on 02-Jan-2020 06:36:33

  • Related Questions & Answers
  • Difference Between Function Overloading and Overriding in C++
  • What is overriding and overloading under polymorphism in java?
  • PHP Overloading
  • Method overloading v/s method overriding in Java
  • What is the difference between method overloading and method overriding in Java?
  • Function overloading and return type in C++
  • Function overloading and const keyword in C++
  • What is method overloading in PHP?
  • What is function overloading in JavaScript?
  • What is the difference between function overriding and method hiding in C#?
  • Overriding in C#
  • str_starts_with and str_ends_with function in PHP 8
  • Increment ++ and Decrement -- Operator Overloading in C++
  • method overloading and type promotion in Java
  • Method Overloading and null error in Java


Method Overloading is a concept of Object Oriented Programming which helps in building the composite application in an easy way. Function overloading or method overloading is a feature that permits making creating several methods with a similar name that works differently from one another in the type of the input parameters it accepts as arguments.

The above concept is fine for other programming languages and it is called static polymorphic i.e method overloading.

Example

Let's understand through an example.

<?php
   class machine {
      function doTask($var1){
         return $var1;
      }
      function doTask($var1,$var2){
         return $var1 * $var1 ;
      }
   }
   $task1 = new machine();
   $task1->doTask(5,10);
?>

Output:

Error

Explanation:

This will generate an error since php will say you have declared this method twice.
But Other programming languages says , doTask($var1) and doTask($var1,$var2) are overloaded methods. To call the latter, two parameters must be passed, whereas the former requires only one parameter.
so this behavior i.e decision to call a function at coding time is known as static polymorphic i.e method overloading.

Let's discuss how to achieve method overloading related to PHP5.In the case of PHP, we have to utilize PHP's magic methods __call() to achieve method overloading.

In PHP overloading means the behavior of method changes dynamically according to the input parameter. In this tutorial, we will understand those perceptions. Let's discuss the __call() method.

__call():

If a class execute __call(), then if an object of that class is called with a method that doesn't exist then__call() is called instead of that method.

Example

Let's understand method overloading with an example.

<?php
   class Shape {
      const PI = 3.142 ;
      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(3);
   $rect = new Shape();
   echo $rect->area(8,6);
?>

Output:

9.426
48

Explanation:

Here area() method is created dynmically and executed with the help of magic method __call() and it's behaviour changes according to pass of parametrs as object.

Method overriding in php tutorialspoint

Updated on 31-Dec-2019 08:29:38

  • Related Questions & Answers
  • What is method overloading in C#?
  • What is method overloading in Java?
  • PHP Overloading
  • What is runtime polymorphism or dynamic method overloading?
  • What is the difference between method overloading and method hiding in Java?
  • What is the difference between method overloading and method overriding in Java?
  • What is overloading in C#?
  • What is Overloading in Java?
  • Method overloading in Java
  • What is constructor overloading in Java?
  • What is function overloading in JavaScript?
  • What is overloading? What happens if we overload a main method in java?
  • Function Overloading and Overriding in PHP
  • Using Method Overloading in Java
  • What is overloading a unary operator in C++?

What is method overriding in PHP?

In function overriding, both parent and child classes should have same function name with and number of arguments. It is used to replace parent method in child class. The purpose of overriding is to change the behavior of parent class method. The two methods with the same name and same parameter is called overriding.

What is Overloading and overriding in PHP?

Method overloading occurs when two or more methods with same method name but different number of parameters in single class. PHP does not support method overloading. Method overriding means two methods with same method name and same number of parameters in two different classes means parent class and child class.

What is Overloading OOP PHP?

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. All overloading methods must be defined as Public.

What is difference between Overloading and overriding?

What is Overloading and Overriding? When two or more methods in the same class have the same name but different parameters, it's called Overloading. When the method signature (name and parameters) are the same in the superclass and the child class, it's called Overriding.