I started off OOP with Java, and now I'm getting pretty heavy into PHP. Is it possible to create multiples of a function with different arguments like in Java? Or will the interpreted / untyped nature of the language prevent this and cause conflicts?
Function overloading or method overloading is the ability to create multiple functions of the same name with different implementations. In simple words, “Overloading means declaring a function multiple times with a different set of parameters”.
Here is an explanation in more high level terms: Java supports Method overloading which is what you are referring to when you talk about function with the same name but different arguments. Since PHP is a dynamically typed language, this is not possible.
PHP native functions According to the manual, PHP functions may accept up to 12 arguments.
The varargs functionality allows you to pass any number of arguments to a method. The method must be set up with the type of data and a variable name to hold the elements. You can add more parameters to the method, but the varargs statement must be the last one.
Everyone else has answers with good code explanations. Here is an explanation in more high level terms: Java supports Method overloading which is what you are referring to when you talk about function with the same name but different arguments. Since PHP is a dynamically typed language, this is not possible. Instead PHP supports Default arguments which you can use to get much the same effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With