41

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?

4
  • 28
    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. Sep 20, 2012 at 4:33
  • 3
    @CharlesSalvia That would be a good answer.
    – apaderno
    Sep 20, 2012 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, 2012 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, 2013 at 18:19

2 Answers 2

18

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.

-1

Also remember:

  • Java (et seq) is a compiled language. "The source-code that you write is turned, once, into "an equivalent binary form" which is the actual thing which drives product execution. The decisions thus made "at compile time" are binding, and the source-code is never seen.

  • PHP, Perl, Ruby, JavaScript (et seq) are interpreted languages, which work directly from source-code – "each and every time." There is no [fixed ...] "compile time."

The differences between these two equally-valid language implementation approaches produce "nuances" on the resulting languages, such as this one. Neither one is right, and neither one is wrong. But, both are born of "software engineering necessity."

2
  • That is wrong. Compiled vs interpreted is a question of implementation, not language. Very few implementations nowadays are pure interpreters in the sense that you describe it. The most common implementations of all the languages you listed (including Java!) are both compilers and interpreters, in the sense that they first compile source code into object code or byte code, which is then run by an interpreter or virtual machine. Most importantly, "compile time" in this context is about language semantics rather than implementation, and all of these languages do provide compile time semantics.
    – Jasmijn
    Apr 3, 2021 at 19:26
  • PHP not supporting function overloading is a choice of semantics, which is not constrained by its implementations.
    – Jasmijn
    Apr 3, 2021 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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