954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Overloading PHP Functions

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)

paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 13
Solved Threads: 4
 

Found this quote:

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

and this quote:Method overloading is however permitted.

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

function ech() {
$a = func_get_args();
for( $t=0;$tech(0,1,2,3,4,5,6,7,8,9);

?>

// output:
// 0123456789

on this page

DanceInstructor
Posting Whiz
368 posts since Feb 2005
Reputation Points: 17
Solved Threads: 14
 

//------------------------
<?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.

harrisonad
Newbie Poster
1 post since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

i want to know that how to use function overloading in php

imran.gnit
Newbie Poster
3 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

what is cake php......
is it new technology.......

imran.gnit
Newbie Poster
3 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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

imran.gnit
Newbie Poster
3 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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

cburke
Newbie Poster
1 post since Jun 2009
Reputation Points: 9
Solved Threads: 0
 
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/

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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.

Petrini
Newbie Poster
3 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

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.

pssubash
Light Poster
31 posts since Feb 2008
Reputation Points: 10
Solved Threads: 4
 

PHP does not support method overloading.PHP support only method over riding

pssubash
Light Poster
31 posts since Feb 2008
Reputation Points: 10
Solved Threads: 4
 

Actually as of php 5.3 you are able to monkey patch core php code. Which is essentially replacing core functions at runtime.

Illustrated by this blog: http://till.klampaeckel.de/blog/archives/105-Monkey-patching-in-PHP.html

mschroeder
Work Harder
Team Colleague
666 posts since Jul 2008
Reputation Points: 279
Solved Threads: 131
 

As I said, it's just a workaround to simulate it.

Petrini
Newbie Poster
3 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

The only type of overloading php supports is with the __set, __get, __isset, __unset, __call, and __staticcall functions.
http://www.php.net/manual/en/language.oop5.magic.php

arionyx
Newbie Poster
6 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

@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.

NuMessiah
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You