Games
Problems
Go Pro!

Ask Professor Puzzler


Do you have a question you would like to ask Professor Puzzler? Click here to ask your question!

"Is it possible to do function overloading in PHP?" ~J

The answer to that question is no. And yes. Let's take a look first at what function overloading is. Let's suppose I want to write a function that creates a random password in PHP. My function might look something like this:

public function password() {
     $password='';
     $length=rand(8,12); //length is random
     for ($i=0;$i<$length;$i++) {
         $password.=chr(65 + rand(0,26));
     }
     return $password;
}

But wait! What if I want to specify the length of the password. Then I'd want a function that looks like this:

public function password($length) {
     $password='';
     for ($i=0;$i<$length;$i++) {
         $password.=chr(65 + rand(0,26));
     }
     return $password;
}

This function works just like the first one, except that I pass a length parameter to the function, instead of selecting the length randomly. In some languages, this is legal to do; I have two functions with the same name, and the call is performed based on whether or not I pass a parameter to it. Unfortunately, this is not currently a feature in PHP. But we can work around it like this:

public function password($length = 'random') {
     $password='';
     if ($length==='random') {
          $length=rand(8,12);
     }
     for ($i=0;$i<$length;$i++) {
         $password.=chr(65 + rand(0,26));
     }
     return $password;
}

How is this function different? It provides a "default" value for the length. If I don't pass a parameter, it ends up being the text 'random.' If no parameter is passed, the function runs the code that selects a random length.

But maybe I want to specify a minimum and maximum password length as parameters. Then I can do this:

public function password($length = 'random',$maxLength=0) {
     $password='';
     if ($length==='random') {
          $length=rand(8,12);
     }
     else {
          if ($maxLength!==0) {
               $length=rand($length,$maxLength);
          }
     }
     for ($i=0;$i<$length;$i++) {
         $password.=chr(65 + rand(0,26));
     }
     return $password;
}

Now, if $maxLength isn't zero, the function knows that I intended to specify a range for the lengths.

If that still isn't good enough, maybe I want to be able to pass the range as an array [minLength,maxLength]. Then I'd change the function to look like this:

public function password($length = 'random',$maxLength=0) {
     $password='';
     if ($length==='random') {
          $length=rand(8,12);
     }
     else {
          if (is_array($length)) {
               $length=rand($length[0],$length[1]);
          }
          else {
               if ($maxLength!==0) {
                    $length=rand($length,$maxLength);
               }
          }
     }
     for ($i=0;$i<$length;$i++) {
         $password.=chr(65 + rand(0,26));
     }
     return $password;
}

We use the fact that PHP can tell us whether the parameter is an array to help us fork the function into a different direction. The function gets more cumbersome each time we add a variation, and at some point it becomes unwieldy, and you're better off just writing different functions. But in this case, we've set up the following overloaded function:

password() creates a random length password
password(n) creates a password of length n
password(n,k) or password([n,k]) creates a password of length somewhere between n and k.

Please note that in this example I didn't do any error checking (for example, making sure that if $length isn't the text "random" it is an actual positive integer, or verifying that in the (n,k) and ([n,k]) variations, that both n and k are positive integers with n < k. All these things should be checked.

Blogs on This Site

Reviews and book lists - books we love!
The site administrator fields questions from visitors.
Like us on Facebook to get updates about new resources
Home
Pro Membership
About
Privacy