What is Overloading Functions? How to Overload Functions in PHP?

Imagine if this is your objective:
  • Go to the grocery store.
You'll think of some way to get to the store, right?
Now, I'll give you the same objective, but with additional information.
  • Go to the grocery store. Use your feet.
  • Go to the grocery store. Use your car.
  • Go to the grocery store. Use a taxi cab.
  • Go to the grocery store. Least expensive method.
  • Go to the grocery store. Fastest method.
  • Go to the grocery store. There won't be any parking when you get there.
So, your objective remains the same, but the actions you perform to reach the objective changes depending on the parameters I'm giving you. Overloading a function is much like this.

With Function Overloading, there is one name for the function, but different types of parameters are passed to the function, and as a result, there are often multiple function definitions with the same name. These separate, but same-named function definitions are typically meant to produce similar results. Each definition computes the result based on the type of arguments it receives. Values passed to the functions could, for example, be either characters or numbers, or perhaps you might have 2 values, but the function is normally waiting to take 3.

Here's another example, we could have written our own function that returns a UNIX epoch timestamp (number of seconds from 1/1/1970 to the specified date: 1262322000 = Jan 1, 2010). These function calls are all legitimate with function overloading:
  • getDate();
  • getDate(2010);
  • getDate(2010, "Mar");
  • getDate(2010, "Mar", 13);

They all are designed to do the same thing, but because the number of parameters being sent can vary, the calculations must be handled differently. And without overloading, the program might crash if it does not receive all 3 values. In some languages, like C++ and JAVA, you would write different function headers and bodies that would accommodate the different numbers of parameters sent to the functions. The one with no parameters becomes the default method. If the number of parameters or type of data being passed to the function does not match any of the possibilities, then an error occurs.

Overloading Functions with PHP

PHP does not appear to support my definition of overloading. Instead the PHP manual talks about ways to compensate for calls to methods that don't exist in an object, or to create member variable value "setters" and "getters" dynamically.

Huh? I know -- a bit advanced and confusing. But PHP does have a useful way to accept different numbers of arguments passed to a function.

And it's easy, too. The function head can be written using default values.

PHP Example

Normally a basic function is written like this:
getDate() {
  return $someValue;

If your are passing, values to the function, you normally write it like this:
getDate ($year, $month, $day) {
  return $someValue;
That assumes you have all three pieces of data ready to go. But what if you have only the year, or just the year or month?

You could still get some kind of response back. How?

Assign default values to the possible variables that might be passed to the function:

getDate($year = 0, $month = "Jan", $day = 1) {
  return $someValue;
Using default values does not overwrite the values that are passed to the function, rather it provides a backup in case no value has been passed by the function call. 

So, some example legal function calls could be:
getDate (); // year=0; month="Jan", day=1
getDate (2010); // year=2010; month="Jan", day=1
getDate (2010,"Aug"); // year=2010; month="Aug", day=1
getDate(2010,"Aug",25); // year=2010; month="Aug", day=25

No comments :

Post a Comment