Coming from C++ background ;)
How can I overload PHP functions?
One function definition if there are any arguments, and another if there are no arguments? Is it possible in PHP? Or should I use if else to check if there are any parameters passed from $_GET and POST?? and relate them?
To over load a function simply do pass parameter as null by default,
Real Overload (Version 3.8) "without extending" + "support closures"
Output:
Real Overload "Without extending" (Version 3.7)
Real Overload (Version 3.5):
Real Overload (Version 3.1):
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.
You can, however, declare a variadic function that takes in a variable number of arguments. You would use
func_num_args()
andfunc_get_arg()
to get the arguments passed, and use them normally.For example:
What about this:
PHP doesn't support traditional method overloading, however one way you might be able to achieve what you want, would be to make use of the
__call
magic method: