Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am wondering if one of the key features of a programming language is to have the ability to overload functions via arguments. I think it is essential in-context of the Object oriented programming.

Is it intentionally left behind and not allowed? Or is overloading not a good practice?

share|improve this question
23  
Function overloading isn't really a feature of OO programming. Sub-class function over-riding for the purpose of polymorphism is usually considered a necessary feature of OO - but not function overloading based on arguments. Python doesn't have argument-based function overloading either. Anyway, function overloading is probably more practical in statically-typed languages where function binding happens at compile time and is based on the type of each function parameter. –  Charles Salvia Sep 20 '12 at 4:33
2  
@CharlesSalvia That would be a good answer. –  kiamlaluno Sep 20 '12 at 6:05
1  
What he said. Also, function overloading is overrated. You don't have to use the same function name for different functions; just give your functions names that are similar but not identical. Operator overloading on the other hand... –  Mr Lister Sep 20 '12 at 6:31
    
Although not an answer to the specific question, i'll post this link which made me better understand HOW to use overloading in PHP. hope it helps –  DiegoDD Dec 20 '13 at 18:19
add comment

2 Answers

Not a "Traditional Overloading" full support, only partial.

A DEFINITION: "Traditional Overloading" provides, when calling method, the ability to have multiple methods with the same name but different quantities and types of arguments. For method declaration, it provides the option to express a separate/isolated declaration for each overloaded function.

Note: the second part of this definition is usually associated with statically-typed programming languages which do type checking and/or arity checking, to choose the correct declaration. PHP is not a statically-typed language, it is dynamic, and use weak typing.


PHP SUPPORT:

  • YES, provides the ability to call multiple methods with the same name but different quantities. See func_get_args and @rajukoyilandy answer, we can use f(x) and f(x,y).

  • YES, provides the ability to call multiple methods with the same name but different types. Se internal use of gettype in the method.

    • BUT for references... Only variables can be passed by reference, you can not manage a overloading for constant/variable detection.
  • NOT provides the ability to declare multiple methods with the same name but different quantities. This is because we can call f(x) and f(x,y) with the same f declaration.

  • NOT provides the ability to declare multiple methods with the same name but different types. This is because PHP compiler must interpret f($integer,$string) as the same function that f($string,$integer).

See also PHP5 overloading documentation for the "non-traditional overloading" explanations.

share|improve this answer
add comment

Who told you PHP doesn't support function overloading?!!!

Actually PHP does support function overloading, but in a different way. PHP's overloading features are different from Java's:

PHP's interpretation of "overloading" is different than most object oriented languages. Overloading traditionally provides the ability to have multiple methods with the same name but different quantities and types of arguments.

Checkout the following code blocks.

Function to find sum of n numbers:

function findSum() {
    $sum = 0;
    foreach (func_get_args() as $arg) {
        $sum += $arg;
    }
    return $sum;
}

echo findSum(1, 2), '<br />'; //outputs 3
echo findSum(10, 2, 100), '<br />'; //outputs 112
echo findSum(10, 22, 0.5, 0.75, 12.50), '<br />'; //outputs 45.75

Function to add two numbers or to concatenate two strings:

function add() {
    //cross check for exactly two parameters passed
    //while calling this function
    if (func_num_args() != 2) {
        trigger_error('Expecting two arguments', E_USER_ERROR);
    }

    //getting two arguments
    $args = func_get_args();
    $arg1 = $args[0];
    $arg2 = $args[1];

    //check whether they are integers
    if (is_int($arg1) && is_int($arg2)) {
        //return sum of two numbers
        return $arg1 + $arg2;
    }

    //check whether they are strings
    if (is_string($arg1) && is_string($arg2)) {
        //return concatenated string
        return $arg1 . ' ' . $arg2;
    }

    trigger_error('Incorrect parameters passed', E_USER_ERROR);
}

echo add(10, 15), '<br />'; //outputs 25
echo add("Hello", "World"), '<br />'; //outputs Hello World

Object Oriented Approach including Method Overloading:

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.

Ref: http://php.net/manual/en/language.oop5.overloading.php

In PHP, overloading means you can add object members at run-time, by implementing some of the magic methods like __set, __get, __call etc.

class Foo {

    public function __call($method, $args) {

        if ($method === 'findSum') {
            echo 'Sum is calculated to ' . $this->_getSum($args);
        } else {
            echo "Called method $method";
        }
    }

    private function _getSum($args) {
        $sum = 0;
        foreach ($args as $arg) {
            $sum += $arg;
        }
        return $sum;
    }

}

$foo = new Foo;
$foo->bar1(); // Called method bar1
$foo->bar2(); // Called method bar2
$foo->findSum(10, 50, 30); //Sum is calculated to 90
$foo->findSum(10.75, 101); //Sum is calculated to 111.75
share|improve this answer
7  
The second bit about what PHP calls "overloading" isn't all that similar. It's just PHP using a well established name for something mostly different, considering it's PHP, it's not that surprising though. –  phant0m Sep 20 '12 at 11:33
3  
That is not function overloading. To truly overload a function, now write the php equivalent of findSum(10, "steve", { 86, "2012-09-20" });. A true overload would allow new parameters of different types. –  Joel Etherton Sep 20 '12 at 12:17
1  
@JoelEtherton - PHP's interpretation of "overloading" is different than most object oriented languages. Overloading traditionally provides the ability to have multiple methods with the same name but different quantities and types of arguments. ref: http://php.net/manual/en/language.oop5.overloading.php 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. –  rajukoyilandy Sep 20 '12 at 12:35
3  
@rajukoyilandy: That's all well and good, but OP's question asks why PHP doesn't implement "traditional" overloading. Your answer does not address that. –  Joel Etherton Sep 20 '12 at 13:31
1  
yes, i want to know why traditional way has been avoided by php. Using a system function like func_get_args() looks bit odd to me in the coding, ideally i would like to declare same function names with different arguments to make sense... –  user1179459 Sep 21 '12 at 0:51
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.