Function Overloading and Overriding 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().
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().
See the above code snippet, here __call function is triggered when invoking inaccessible methods in an object context.
Function overriding is quite easy in PHP. 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’s call function overriding.
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!!!