Life's random bits By b1thunt3r (aka Ishan Jain)…
Function overloading in PHP

Function overloading in PHP

Ishan jain
Overloading functions in PHP in a clever way.

Update: The "Default Parameter" functionality was added in C# 4

A while back i wrote somewhere:

After doing some Java, i do miss “overloading” a function. I hope they will implement it in PHP6… Overloading is creating two functions with same name, but have different amount off arguments passed to them… I did find a trick to go around, using a combination of func_num_args and func_get_args.

Last week i was working on a project in C# and tried to set a default value for a param, but got a error “Default parameter specifier is not permitted”. It was then i realized that PHP do have overloading, and it is done by supplying default values to parameters when defining a function.

# Function Definition
function a($b, $c = false) {
// do something
}

# Function can be accessed by
a("test"); // == a("test", false);
a("test", true);