I know it is possible to overload PHP functions as I have seen this done with many of the prebuilt functions. My question is how do you declare an overloaded function? Everytime I try doing this I always get some error about missing arguments (even if there exsist a function with the same number of arguments)

Found this quote:

PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.[

and this code:

Method overloading is however permitted.

<?php
class A {
       function A() { }

       function ech() {
               $a = func_get_args();
               for( $t=0;$t<count($a); $t++ ) {
                       echo $a[$t];
               }
       }
}       

$test = new A();
$test->ech(0,1,2,3,4,5,6,7,8,9);

?>

// output:
// 0123456789

on this page.

Edited 3 Years Ago by pyTony: fixed formating

//------------------------
<?php
class A{
function A(){};
}
?>
//---------------------

Dance Instructor is only refering to class constructor, not an example of function overloading.

it is sad though, but php doen't support function overloading.

i have to use a .net web service in my php application ...and i have no knowledge how to use .net web service in php but i know how to make a web service in .net...please reply me soon its urgent

Actually, method overloading refers to the ability to use the same method/function name in multiple classes such as:

class test1
{

private $value;

//constructor
function test1(){}

//method
function setValue($value){
  $this->value=$value;
}

}


class test2
{

private $value;

//constructor
function test2(){}

//method
function setValue($value){
  $this->value=$value;
}

}

//in use:

<?php
$test1=new test1(); $test1->setValue(1);
$test2=new test2(); $test2->setValue(2);
?>

As you can see, the same method name was used in both classes and can be called in the same php file. Hope this helps

Comments
Why post on 4+ year old thread? Pointless.

I know it is possible to overload PHP functions as I have seen this done with many of the prebuilt functions. My question is how do you declare an overloaded function? Everytime I try doing this I always get some error about missing arguments (even if there exsist a function with the same number of arguments)

It's all about object overloading.

Read this http://www.onlamp.com/pub/a/php/2005/06/16/overloading.html
and
http://www.devshed.com/c/a/PHP/Overloading-and-ObjectOriented-Programming-with-PHP-5/

PHP isn't strongly typed, making the function overload, as we are used, kinda senseless. In most languages that allow function overloading, you have to differentiate the several overloads by the parameters types.
So there are some workarounds to do it in PHP. We can check the parameters (after setting default values) and 'guess' the best way to treat them, or use stuffs like is_string(), etc., like:

function divide($arg1, $arg2 = 2, $arg3 = 2) {
  if(!is_string($arg1)
    return $number1 / $number2;
  return arg1 . ($arg2 / $arg3);
}

We'll have 4 options:
divide(25) returns 12.5
divide(25, 5) returns 5
divide("Result: ", 25) returns "Result: 12.5"
divide("Result: ", 25, 5) returns "Result: 5"

It's a little boring doing all the checks and validations, but I think it's the only way to simulate it.

Edited 6 Years Ago by Petrini: n/a

PHP isn't strongly typed, making the function overload, as we are used, kinda senseless. In most languages that allow function overloading, you have to differentiate the several overloads by the parameters types.
So there are some workarounds to do it in PHP. We can check the parameters (after setting default values) and 'guess' the best way to treat them, or use stuffs like is_string(), etc., like:

function divide($arg1, $arg2 = 2, $arg3 = 2) {
  if(!is_string($arg1)
    return $number1 / $number2;
  return arg1 . ($arg2 / $arg3);
}

We'll have 4 options:
divide(25) returns 12.5
divide(25, 5) returns 5
divide("Result: ", 25) returns "Result: 12.5"
divide("Result: ", 25, 5) returns "Result: 5"

It's a little boring doing all the checks and validations, but I think it's the only way to simulate it.

On my understanding,this is not method overloading.this is an example for default arguments.method overloading is a class have more than one functions with same name but diffrent function signatures.

@arionyx This is the property overloading. The question in this thread was if it is possible to use method (function) overloading. And the answer is simply: NO.

This article has been dead for over six months. Start a new discussion instead.