Function Overloading and Overriding in PHP concept come from polymorphism of OOP. According to Object Oriented Programming, function overloading means multiple method/function have the same name but different parameters. And in the child class having the same name, same parameters as in its parent it called method overriding.
Generally, PHP doesn’t support function overloading directly like other languages such as C++, JAVA etc. But we can overcome this issue with using the PHP magic method __call(). So let’s see how overloading and overriding in PHP works.
Overloading in PHP
Let’s see where we can’t use function overloading in PHP that we could easily in other language. For example, see the following PHP script.
<?php
/**
* @author: Sohel Rana
* @URI: http://sohelrana.me
* @description: Function overloading error with PHP.
*/
class SocialMedia
{
public function sharaMessage($para)
{
echo "sharaMessage() with one parameter";
}
public function sharaMessage($para1, $para2)
{
echo "sharaMessage() with two parameter";
}
}
$object = new SocialMedia();
$object->sharaMessage('Hello World');
If we convert this snippet of code in C++ or JAVA, then it will be working properly. This is the basic example of function overloading for C++ or JAVA programming language. but this code will not work for the PHP. It will show “PHP Fatal error: Cannot redeclare SocialMedia::sharaMessage()” error message. But if we want to do something like that, then we will have to use PHP magic method __call(). With that magic method you can actually implement method overloading.
Solution:
<?php
/**
* @author: Sohel Rana
* @URL: http://sohelrana.me
* @description: Function overloading in PHP.
*/
class SocialMedia
{
public function __call($funName, $arguments)
{
$funArray = array('sharaMessage', 'sharaMessage2');
if (in_array($funName, $funArray) === false) {
die("Method does not exist");
}
if (count($arguments) === 2) {
$this->sharaMessage2($arguments[0], $arguments[1]);
} elseif (count($arguments) === 1) {
$this->sharaMessage($arguments[0]);
} else {
echo "Unknown method";
return false;
}
}
public function sharaMessage($para)
{
echo "sharaMessage() with one parameter";
}
public function sharaMessage2($para1, $para2)
{
echo "sharaMessage() with two parameter";
}
}
$object = new SocialMedia();
$object->sharaMessage('hello');
$object->sharaMessage2('hello', "hello2");
$object->sharaMessage3('hello');
See the above code snippet, here __call function is triggered when invoking inaccessible methods in an object context. If you have question about overloading in PHP, write a comment.
Function Overriding
Function overriding in PHP is quite easy. Overriding is the process of modifying something of the inherited method. In the OOP sense if a parent class has a function (e.g testMethod()) and another class inherited from that parent class. And that child class has same method (e.g testMethod()), that means child class’s testMethod() overring the parent class’s testMethod(). That is called function overriding. Don’t mixed it up with function overloading.
Example:
<?php
/**
* @author: Sohel Rana
* @URL: http://sohelrana.me
* @description: Function uverriding PHP.
*/
class ParentClass {
public function sharing() {
echo "sharing parent class";
}
}
class ChildClass extends ParentClass {
public function sharing() {
echo "sharing child ";
}
}
$obj1 = new ParentClass();
$obj2 = new ChildClass();
$obj1->sharing();
$obj2->sharing();
From the above code snippet, you can see ParentClass class sharing() function. ChildClass inherited from ParentClass. And ChildClass also has the same function sharing(). So ChildClass’s sharing() overriding his ParentClass class.
I hope this article will help you to understand the function overloading and overriding in PHP. Happy Coding!!! To read more PHP related articles click here.