PHP Function Overloading

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);

Method overloading in PHP is bad practice?

The traditional approach would be:

class Aclass {
public function doStuff(int $a) {
// return other stuff
}

public function doStuff(float $a) {
// return other stuff
}
}

Notice the same function name just different type for the paramaters.

Your first approach is usually the way to go if you want to simulate overloading in php. Generally speaking you don't really need overloading in PHP since it's loosely type and by definition you can't really have overloading.


To answer your question. If you really need to have overloading, then your first approach seems appropriate. Use a switch to make it more readable.

Disclaimer: While you can do that, I don't recommend it. Heck, you can do just about anything if you want to. It doesn't mean you should.

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 PHP function overloading for?

PHP's meaning of overloading is different than Java's. In PHP, overloading means that you are able to add object members at runtime, by implementing some of the __magic methods, like __get, __set, __call, __callStatic. You load objects with new members.

Overloading in PHP provides means to
dynamically "create" properties and
methods. These dynamic entities are
processed via magic methods one can
establish in a class for various
action types.

An example:

class Foo
{
public function __call($method, $args)
{
echo "Called method $method";
}
}

$foo = new Foo;

$foo->bar(); // Called method bar
$foo->baz(); // Called method baz

And by the way, PHP supports this kind of overloading since PHP 4.3.0. The only difference is that in versions prior to PHP 5 you had to explicitly activate overloading using the overload() function.

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

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);

PHP Function Overloading - How to?

You can add optional parameters in the function declaration like:

public function makeTweet( $DatabaseObject, $TextObject, $MessageObject = null)

Now you can either do:

$obj->makeTweet($db, $text, $messageObj);

or

$obj->makeTweet($db, $text);

This is the closest you can get in PHP.

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.

Function Overloading in CodeIgniter

In PHP to overload functions use optional parameters. An example might be:

function load_view($var = null) {
if (!empty($var)) {
$this->load->model('data');
$data = $this->data->getInfo($var);
$this->load->view('view', $data);
} else {
$this->load->view('view');
}
}


Related Topics



Leave a reply



Submit