What Is Function Overloading and Overriding in PHP

What is function overloading and overriding in php?

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"
?>

what is function overloading and overriding in OOPS, explain in layman terms

Method Overriding

Method Overloading

Simple Explanation

Method Overriding is when a method defined in a superclass or interface is re-defined by one of its subclasses, thus modifying/replacing the behavior the superclass provides. The decision to call an implementation or another is dynamically taken at runtime, depending on the object the operation is called from. Notice the signature of the method remains the same when overriding.

Method Overloading is unrelated to polymorphism. It refers to defining different forms of a method (usually by receiving different parameter number or types). It can be seen as static polymorphism. The decision to call an implementation or another is taken at coding time. Notice in this case the signature of the method must change.

PHP function overloading

You cannot overload PHP functions. Function signatures are based only on their names and do not include argument lists, so you cannot have two functions with the same name. Class method overloading is different in PHP than in many other languages. PHP uses the same word but it describes a different pattern.

You can, however, declare a variadic function that takes in a variable number of arguments. You would use func_num_args() and func_get_arg() to get the arguments passed, and use them normally.

For example:

function myFunc() {
for ($i = 0; $i < func_num_args(); $i++) {
printf("Argument %d: %s\n", $i, func_get_arg($i));
}
}

/*
Argument 0: a
Argument 1: 2
Argument 2: 3.5
*/
myFunc('a', 2, 3.5);

Differentiate between function overloading and function overriding

You are putting in place an overloading when you change the original types for the arguments in the signature of a method.

You are putting in place an overriding when you change the original Implementation of a method in a derived class.

Does PHP have function overloading feature like C#, JAVA, C++

Simply put: no. In PHP, a method signature doesn't include it's parameter set, only it's name. Therefore, two methods with the same name but different parameters are actually considered equal (and thus an error results).

PHP does have a different process which it calls method overloading, but it's a different approach to the problem. In PHP, overloading is a means by which methods and properties can be dynamically created on an object at runtime. An example using the __call method follows.

The __call method of a class will be invoked when there is no method matching the method name that was called inside of the class. It will receive the method name, and an array of the arguments.

class OverloadTest {
public function __call($method, $arguments) {
if ($method == 'overloadedMethodName') {
switch (count($arguments)) {
case 0:
return $this->overloadedMethodNoArguments();
break;
case 1:
return $this->overloadedMethodOneArgument($arguments[0]);
break;
case 2:
return $this->overloadedMethodTwoArguments($arguments[0], $arguments[1]);
break;
}
}
}

protected function overloadedMethodNoArguments() { print "zero"; }
protected function overloadedMethodOneArgument($one) { print "one"; }
protected function overloadedMethodTwoArguments($one, $two) { print "two"; }
}

$test = new OverloadTest();
$test->overloadedMethodName();
$test->overloadedMethodName(1);
$test->overloadedMethodName(1, 2);

Alternatively, you can provide a function with default arguments which will effectively allow for syntax that looks like overloading. Such as:

function testFunction($one, $two = null, $three = null) {

}

testFunction(1);
testFunction(1, 2);
testFunction(1, 2, 3);

And, finally, as for the third method, you can of course always access the arguments as an array within the function itself

function variableFunction() {
$arguments = func_get_args();

switch (count($arguments)) {
// ...
}
}

variableFunction();
variableFunction(1, 2, 3, 4, 5, 6, 7, 8, 9);

method overloading in php?

PHP does not support overloading of methods/functions, but it does support variable number of arguments via func_num_args and func_get_arg

PHP Overriding methods rules

The rule is that a method signature must be compatible with the method it overrides. Let's look at the two methods in your hierarchy:

protected function playing($game = 'ball');

public function playing($gameType = 'chess', $location = 'backyard');

The changes:

  1. Visibility: protected -> public; increasing the visibility is compatible (the opposite will cause errors).

  2. Arguments: no change (same number of required arguments and maximum number of arguments)



Related Topics



Leave a reply



Submit