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.

Read Also:  PHP Class Inheritance call parent constructor

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:  encapsulation in php

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.

Read Also:  jQuery Print Dynamic Content using PHP MySQL Example

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP MySQL Dynamic SQL Insert Query
  2. Read Also:  PHP Array Remove specific value Example
  3. Read Also:  How to Get Last Executed Query in Codeigniter?
  4. Read Also:  Concept of Encapsulation in PHP OOP Example
  5. Read Also:  Insert update delete Using Laravel 5.2 Ajax CRUD laravel without refresh page
  6. Read Also:  Laravel 6 Ajax CRUD Example
  7. Read Also:  Laravel Return Ajax JSON Response Example
  8. Read Also:  PHP Parse URL Querystring Into Array
  9. Read Also:  PHP Download Remote File from URL using CURL
  10. Read Also:  Converting JSON Object into Javascript PHP array
CLOSEX