What is function overriding in php?

Today, We want to share with you function overriding in php.In this post we will show you php override function with different parameters, hear for overloading and overriding in php in hindi we will give you demo and example for implement.In this post, we will learn about Polymorphism In PHP Object-Oriented Programming with an example.

Overloading and Overriding in PHP with Examples

simple defination Overriding is only pertinent to derived classes, where the main parent class has defined a some method as well as the derived some class wishes to override that some method.

Function Overloading:

<?php 

class shape { 
	

	function __call($name_of_function, $arguments) { 
			

		if($name_of_function == 'area') { 
			
			switch (count($arguments)) { 
					
 
				case 1: 
					return 3.14 * $arguments[0]; 
						
				case 2: 
					return $arguments[0]*$arguments[1]; 
			} 
		} 
	} 
} 
	 
$s = new Akar; 
	
echo($s->area(2)); 
echo "\n"; 
	 
echo ($s->area(4, 2)); 
?> 

Function Overriding:

<?php 

class P { 
	
	function getData() { 
		echo "Parent"; 
	} 
} 

class C extends P { 
	 
	function getData() { 
		echo "\nChild"; 
	} 
} 

$p = new P; 

$c= new C; 
 
$p->getData(); 

$c->getData(); 
?> 

An example of overriding:

<?php

class Data {
   function getData() {
      return "Data";
   }
}

class List extends Data {
   function getData() {
      return "List";
   }
}

$data = new Data;
$list = new List;
echo($data->getData()); //"Data"
echo($list->getData()); //"List"
?>

PHP overriding methods

here you can learn to Function overriding is used when a class has more methods as well as more derived class wants same functions or methods with different types of the behavior. Therefor, using overriding you can best way to updating the behavior of parent class.

Read Also:  WooCommerce REST API CRUD Tutorial From Scratch

as well as Function overriding can be step by step useful by extending a class as well as rewriting a function which existed in the more parent class.

class Akar {
    function myAkar() {
        return "Akar";
    }
}
class Chorash extends Akar {
    function myAkar() {
        return "Chorash";
        }
}
$shape = new Akar;
$square = new Chorash;
echo($shape->myAkar()); //"Akar"
echo($square->myChorash()); //"Chorash"
?>

I hope you get an idea about function overriding in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.