In PHP, what do you mean by function overloading and function overriding. and what is the difference between both of them? couldn't figure out what is the difference between them.
-
You can use overloadable class in this link: http://stackoverflow.com/questions/4697705/php-function-overloading/27231803#27231803 – Hisham Dalal Dec 01 '14 at 15:53
11 Answers
Overloading is defining functions that have similar signatures, yet have different parameters. Overriding is only pertinent to derived classes, where the parent class has defined a method and the derived class wishes to override that method.
In PHP, you can only overload methods using the magic method __call
.
An example of overriding:
<?php
class Foo {
function myFoo() {
return "Foo";
}
}
class Bar extends Foo {
function myFoo() {
return "Bar";
}
}
$foo = new Foo;
$bar = new Bar;
echo($foo->myFoo()); //"Foo"
echo($bar->myFoo()); //"Bar"
?>

- 161,348
- 33
- 346
- 320
-
32
-
20
-
53
-
3Thanks for the idea of optional parameters, It's a really helpful hint – Rudolf Real Aug 27 '12 at 16:23
-
3__call() is triggered when inaccessible methods are called in objects context __callStatic() megic method is triggered when inaccessible methods are called in static context. Follow this to get a more detailed example and explanation of Overloading in PHP context: [PHP Overloading](http://php.net/manual/en/language.oop5.overloading.php) – Code Buster Sep 04 '13 at 19:08
-
2@b01 Indeed it does substitute; much in the same way one can substitute a banana for a screwdriver. It could work, and might work, but it kinda sorta mostly doesn't. – Dan Lugg Sep 05 '13 at 20:05
-
1I was here to say the same that Dan said.. Overloading lets you create DIFFERENT FUNCTIONS with the same name... depending on which params yo pass-by it will use one or another... but optional parameters lets you create ONE FUNCTION with more or less params.. which will lead to further parameter validation and different behaviours of the function depending on the params.. and that is not very intuitive nor productive... – KnF Aug 22 '14 at 23:21
-
1Also need to note: `parent::myFoo();` inside Bar->myFoo() calls Foo->myFoo() in Bar. – Fandi Susanto Apr 20 '16 at 08:25
-
1@b01 You're getting it wrong. PHP does not substitutes overloading via optional parameters. If you pass optional parameters to a function, then, still the function will receive a defined number of arguments(passed arguments + optional arguments). This is because optional arguments are still defined while declaring the function. But in overloading, you do not receive a fixed number of arguments. Signature is same but number of arguments is different. Actually PHP DOES NOT SUPPORT overloading concept. – Ashish Choudhary Aug 01 '16 at 01:03
-
As @Andrew Moore said. Its a dirty hack. You make a magic method(__call) and play with any undefined function does not mean its overloading. If overloading has anything to do with PHP, its because of the functions like ''func_get_args()" and "func_num_args()" which really let people make PHP behave like its performing overloading. – Ashish Choudhary Aug 01 '16 at 01:03
-
Optional parameters or `func_get_args()` or `func_num_args()` have absolutely nothing to do with overloading. Real overloading means you can declare `myFunc(\DateTime $foo)` and `myFunc(\Vendor\Package\Model $bar, int $baz)` that behave totally different. Overloading means having different types (*and* possibly also a different number) of arguments *and* a different implementation. – Foo Bar Aug 29 '17 at 19:53
Function overloading is not supported by PHP. It occurs when you define the same function name twice (or more) using different set of parameters. For example:
class Addition {
function compute($first, $second) {
return $first+$second;
}
function compute($first, $second, $third) {
return $first+$second+$third;
}
}
In the example above, the function compute
is overloaded with two different parameter signatures. *This is not yet supported in PHP. An alternative is to use optional arguments:
class Addition {
function compute($first, $second, $third = 0) {
return $first+$second+$third;
}
}
Function overriding occurs when you extend a class and rewrite a function which existed in the parent class:
class Substraction extends Addition {
function compute($first, $second, $third = 0) {
return $first-$second-$third;
}
}
For example, compute
overrides the behavior set forth in Addition
.

- 156,878
- 40
- 214
- 345

- 93,497
- 30
- 163
- 175
-
9Except function overloading is not supported in PHP, AFAIK. Also, as an unrelated side note, I'm not sure why you would have a `Subtraction` class extend the `Addition` class. :) – Sasha Chedygov Jun 08 '10 at 04:38
-
6@musicfreak: 12:40 AM local time... Couldn't think of a better example. – Andrew Moore Jun 08 '10 at 04:40
-
-
5*This is not yet supported in PHP* and probably never will, as PHP is weakly-typed language and nothing is going to change in this case. – Crozin Aug 31 '10 at 00:34
-
3Overloading is not only when you define the same function name twice (or more times) using different set of parameters. Generally, overloading is also when you define the same function name twice (or more times) using the same number of parameters but of different types. Since in PHP there's no variable type declaration (like in Java) this generalization does not matter. I'm just mentioning this for the sake of preciseness what overloading is. – sbrbot Jan 24 '13 at 13:07
-
2
-
Strictly speaking, there's no difference, since you cannot do either :)
Function overriding could have been done with a PHP extension like APD, but it's deprecated and afaik last version was unusable.
Function overloading in PHP cannot be done due to dynamic typing, ie, in PHP you don't "define" variables to be a particular type. Example:
$a=1;
$a='1';
$a=true;
$a=doSomething();
Each variable is of a different type, yet you can know the type before execution (see the 4th one). As a comparison, other languages use:
int a=1;
String s="1";
bool a=true;
something a=doSomething();
In the last example, you must forcefully set the variable's type (as an example, I used data type "something").
Another "issue" why function overloading is not possible in PHP: PHP has a function called func_get_args(), which returns an array of current arguments, now consider the following code:
function hello($a){
print_r(func_get_args());
}
function hello($a,$a){
print_r(func_get_args());
}
hello('a');
hello('a','b');
Considering both functions accept any amount of arguments, which one should the compiler choose?
Finally, I'd like to point out why the above replies are partially wrong; function overloading/overriding is NOT equal to method overloading/overriding.
Where a method is like a function but specific to a class, in which case, PHP does allow overriding in classes, but again no overloading, due to language semantics.
To conclude, languages like Javascript allow overriding (but again, no overloading), however they may also show the difference between overriding a user function and a method:
/// Function Overriding ///
function a(){
alert('a');
}
a=function(){
alert('b');
}
a(); // shows popup with 'b'
/// Method Overriding ///
var a={
"a":function(){
alert('a');
}
}
a.a=function(){
alert('b');
}
a.a(); // shows popup with 'b'

- 61,812
- 21
- 118
- 158

- 27,509
- 17
- 111
- 155
-
2PHP 5.x.x does not support overloading this is why PHP is not fully OOP. – PHP Ferrari Dec 27 '10 at 18:19
-
30It isn't that PHP does not support overloading, it is that it can't make sense. The issue is due to PHP using dynamic types. In fact, Javascript doesn't support overloading either, and it is still OOP. The "PHP is not fully OOP" fud only exists because some people decided to use a check-list to judge what's OOP or not. – Christian Dec 28 '10 at 12:15
-
@ringø Nope, named functions in PHP are immutable. Once you create a named function in PHP, there's no way you can modify its internal behaviour (overriding). Unless you meant method overriding, which is something else.. You should probably read the whole answer before commenting on the first line. :) – Christian Nov 28 '16 at 10:52
-
@Christian Well, of course in PHP (and many other languages) [overriding occurs in a class](http://stackoverflow.com/a/11912039/338904). So, yes, we're talking about methods - even though each of them is prefixed with the keyword *function*. But indeed I should have read the whole answer first... – Déjà vu Nov 28 '16 at 14:09
Overloading Example
class overload {
public $name;
public function __construct($agr) {
$this->name = $agr;
}
public function __call($methodname, $agrument) {
if($methodname == 'sum2') {
if(count($agrument) == 2) {
$this->sum($agrument[0], $agrument[1]);
}
if(count($agrument) == 3) {
echo $this->sum1($agrument[0], $agrument[1], $agrument[2]);
}
}
}
public function sum($a, $b) {
return $a + $b;
}
public function sum1($a,$b,$c) {
return $a + $b + $c;
}
}
$object = new overload('Sum');
echo $object->sum2(1,2,3);

- 1,531
- 17
- 21
-
-
This is not method, function overloading, while you're using different method name for each puporse – TomSawyer Jan 24 '17 at 09:09
Although overloading paradigm is not fully supported by PHP the same (or very similar) effect can be achieved with default parameter(s) (as somebody mentioned before).
If you define your function like this:
function f($p=0)
{
if($p)
{
//implement functionality #1 here
}
else
{
//implement functionality #2 here
}
}
When you call this function like:
f();
you'll get one functionality (#1), but if you call it with parameter like:
f(1);
you'll get another functionality (#2). That's the effect of overloading - different functionality depending on function's input parameter(s).
I know, somebody will ask now what functionality one will get if he/she calls this function as f(0).

- 6,169
- 6
- 43
- 74
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.

- 41
- 1
I would like to point out over here that Overloading in PHP has a completely different meaning as compared to other programming languages. A lot of people have said that overloading isnt supported in PHP and by the conventional definition of overloading, yes that functionality isnt explicitly available.
However, the correct definition of overloading in PHP is completely different.
In PHP overloading refers to dynamically creating properties and methods using magic methods like __set() and __get(). These overloading methods are invoked when interacting with methods or properties that are not accessible or not declared.
Here is a link from the PHP manual : http://www.php.net/manual/en/language.oop5.overloading.php

- 57
- 1
- 7
Overloading: In Real world, overloading means assigning some extra stuff to someone. As as in real world Overloading in PHP means calling extra functions. In other way You can say it have slimier function with different parameter.In PHP you can use overloading with magic functions e.g. __get, __set, __call etc.
Example of Overloading:
class Shape {
const Pi = 3.142 ; // constant value
function __call($functionname, $argument){
if($functionname == 'area')
switch(count($argument)){
case 0 : return 0 ;
case 1 : return self::Pi * $argument[0] ; // 3.14 * 5
case 2 : return $argument[0] * $argument[1]; // 5 * 10
}
}
}
$circle = new Shape();`enter code here`
echo "Area of circle:".$circle->area()."</br>"; // display the area of circle Output 0
echo "Area of circle:".$circle->area(5)."</br>"; // display the area of circle
$rect = new Shape();
echo "Area of rectangle:".$rect->area(5,10); // display area of rectangle
Overriding : In object oriented programming overriding is to replace parent method in child class.In overriding you can re-declare parent class method in child class. So, basically the purpose of overriding is to change the behavior of your parent class method.
Example of overriding :
class parent_class
{
public function text() //text() is a parent class method
{
echo "Hello!! everyone I am parent class text method"."</br>";
}
public function test()
{
echo "Hello!! I am second method of parent class"."</br>";
}
}
class child extends parent_class
{
public function text() // Text() parent class method which is override by child
class
{
echo "Hello!! Everyone i am child class";
}
}
$obj= new parent_class();
$obj->text(); // display the parent class method echo
$obj= new parent_class();
$obj->test();
$obj= new child();
$obj->text(); // display the child class method echo

- 85
- 2
- 4
There are some differences between Function overloading & overriding though both contains the same function name.In overloading ,between the same name functions contain different type of argument or return type;Such as: "function add (int a,int b)" & "function add(float a,float b); Here the add() function is overloaded. In the case of overriding both the argument and function name are same.It generally found in inheritance or in traits.We have to follow some tactics to introduce, what function will execute now. So In overriding the programmer follows some tactics to execute the desired function where in the overloading the program can automatically identify the desired function...Thanks!
Overloading: Declaring a function multiple times with a different set of parameters like this:
<?php
function foo($a) {
return $a;
}
function foo($a, $b) {
return $a + $b;
}
echo foo(5); // Prints "5"
echo foo(5, 2); // Prints "7"
?>
Overriding: Replacing the parent class's method(s) with a new method by redeclaring it like this:
<?php
class foo {
function new($args) {
// Do something.
}
}
class bar extends foo {
function new($args) {
// Do something different.
}
}
?>

- 3,721
- 11
- 40
- 82
PHP 5.x.x does not support overloading this is why PHP is not fully OOP.

- 15,754
- 27
- 83
- 149
-
19I don't know where you got the idea that overloading a requirement for Object Oriented Programming. – drewish Sep 02 '11 at 21:43
-
6@drewish because parametric polymorphism is one of the most important features of OOP? – Davor May 16 '12 at 17:29
-
1@drewish if you count polymorphism as one of the main stagnation of OOP. Then overloading ostensibly is out there for the compilation. However, method overloading is the compile time polymorphism and also known as static polymorphism. – Yasir Arefin Mar 06 '14 at 13:07