Can you have two functions with the same name in a php class why or why not?
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? Show
Luís Cruz 14.4k16 gold badges69 silver badges98 bronze badges asked Jan 27, 2010 at 14:33
2 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. answered Jan 27, 2010 at 14:48
PoindexterPoindexter 2,37616 silver badges19 bronze badges 1 If you are dealing with classes you can overload methods with
Then you can do:
This might not be the best example but But it is not the same or as easy as in Java.
answered Jan 27, 2010 at 14:54
Felix KlingFelix Kling 769k171 gold badges1068 silver badges1114 bronze badges 6 Short answer: No. There can only be one function with a given name. Longer answer: You can do this by creating a convoluted include system that includes the function with the right number of arguments. Or, better yet, you can take advantage of PHP allowing default values for parameters and also a variable amount of parameters. To take advantage of default values just assign a value to a parameter when defining the function:
It's common practice to put parameters with default values at the end of the function declaration since they may be omitted when the function is called and makes the syntax for using them clearer:
You can also send a variable amount of parameters by using
answered Jan 27, 2010 at 14:40
John CondeJohn Conde 214k98 gold badges447 silver badges489 bronze badges Following isn't possible with php
Instead do this way
Kukeltje 12.1k4 gold badges21 silver badges46 bronze badges answered Jan 27, 2010 at 14:40
YOUYOU 116k32 gold badges184 silver badges216 bronze badges 1 Like everyone else said, it's not supported by default. Felix's example using Otherwise, if you are using classes that inherit from each other you can always overload the method names in your child classes. This also allows you to call the parent method. Take these classes for example...
Then you can call them like so...
And your result would be... Loading Facebook Account: 123 Loading Twitter Account: 123 answered Feb 13, 2011 at 22:44
brooxbroox 2,91030 silver badges24 bronze badges No this isn't possible, because PHP cannot infer from the arguments which function you want (you don't specify which types you expect). You can, however, give default values to arguments in php. That way the caller can give different amounts of arguments. This will call the same function though. Example is:
This gives a default of true if 0 arguments are given, and takes the calling value if 1 argument is given.
Peter Gluck 8,1001 gold badge37 silver badges35 bronze badges answered Jan 27, 2010 at 14:37
ThirlerThirler 19.7k13 gold badges64 silver badges89 bronze badges I know it's a bit old issue, but since php56 you can:
ref: http://php.net/manual/en/functions.arguments.php answered Apr 12, 2017 at 7:40
Overloading is not possible in PHP but you can get around it to some extend with default parameter values as explained in other responses. The limit to this workaround is when one wants to overload a function/method according to the parameter types. This is not possible in PHP, one need to test the parameter types yourself, or write several functions. The functions min and max are a good example of this : if there is one parameter of array type it returns the min/max of the array, otherwise it returns the min/max of the parameters. answered Jun 16, 2011 at 9:35
I had the idea of something like: function process( $param1 , $type='array' ) { switch($type) { case 'array': // do something with it break; case 'associative_array': // do something with it break; case 'int_array': // do something with it break; case 'string': // do something with it break; // etc etc... } } answered May 30, 2017 at 12:25
IanIan 9002 gold badges8 silver badges19 bronze badges I have got 2 methods,
answered Aug 24, 2018 at 0:21
Can you have two functions with the same name in a PHP class?No. PHP does not support classic overloading. (It does implement something else that is called overloading.)
Can two functions have the same name?Yes, it's called function overloading. Multiple functions are able to have the same name if you like, however MUST have different parameters.
Why function overloading is not supported in PHP?You cannot overload PHP functions. Function signatures are based only on their names and do not include argument lists, so you cannot have two functions with the same name. Class method overloading is different in PHP than in many other languages. PHP uses the same word but it describes a different pattern.
What are the two types of functions in PHP?PHP provides us with two major types of functions:. Built-in functions : PHP provides us with huge collection of built-in library functions. ... . User Defined Functions : Apart from the built-in functions, PHP allows us to create our own customised functions called the user-defined functions.. |