PHP Function Overloading

Posted by: John in PHP on

As a college student I am required to use different programming languages depending on the class I am taking. My first two semesters I used Java (while the professor attempted to teach us object oriented programming) and my last semester I used C++. Nonetheless, both languages have vast support for object oriented programming. Several days ago, while working on my php database structure I attempted to overload my connect() function.

What is function overloading?
Function overloading is the ability to name more than one function with the same name and have different input parameters along with different return values.

This is the exact functionality I wished to replicate with my connect() function. If the user supplied three parameters (host, username, and password), I wanted it to call the mysql_connect() function directly. However, I wanted to give the user the ability to supply a single parameter, a DSN (data source name) which looks something like mysql://user:password@localhost/db_name. My overloaded connect function would then parse the DSN and extract the necessary parameters and finally call the mysql_connect() function. However, I was greeted with this error:
Fatal error: Cannot redeclare class::function() in path on line line


After a quick googling, much to my surprise, PHP does not allow for function overloading. Without overloading, I was forced to create a function, and then set the additional two parameters to null:
 
  1. function connect($host, $username=null, $password=null)
  2. {
  3.         if($username == null && $password == null) {
  4.                 //parse the dsn
  5.         } else {
  6.         mysql_connect($host, $username, $password);
  7.         }
  8. }
Although, in my opinion, not as elegant as function overloading, it does the same job.


The above was not the reason for this blog. It is to introduce a few new function I recently found while looking at the source of another framework. The functions are func_get_arg(), func_get_args(), and func_num_args. func_num_args will return the number of arguments passed to the function (despite declaring any in the function header). If you haven't noticed, now would be a good time for me to point out what I consider an abscurity. PHP will complain if you pass too few arguments to a function, but won't if you pass too many:
 
  1. function foo($arg1, $arg2) {
  2.  
  3. }
  4.  
  5. foo($bar); //this will cause issues
  6. foo($bar, $bat, $baz); //this will NOT cause any issues
Using this, I can redesign my connect() function to look something like:
 
  1. function connect() {
  2.        if(func_num_args() == 3) {
  3.                mysql_connect($func_get_arg[0], $func_get_arg[1], $func_get_arg[2]);
  4.                return;
  5.        }
  6.     //use dsn
  7.     $args = parseDSN($func_get_arg[0]);
  8.     mysql_connect($args[0], $args[1], $args[2]);
  9. }
I am not sure if this method is any more elegant or more optimal than the previous solution. However, these functions can be useful, but should not be overused.


Trackback(0)
Comments (4)add comment

Jordan said:

I prefer to use the first method (vs func_get_arg) for documentation purposes. In 3 months if you came back to your code it would be a lot easier to read:

connect($host, $username=null, $password=null)

than


connect() extracting the func_get_arg.
 
report abuse
vote down
vote up
June 29, 2008 | url
Votes: +0

John said:

I agree, but then the variable name $host becomes misleading. Nonetheless, PHP is missing a very important OOP aspect :/
 
report abuse
vote down
vote up
June 29, 2008
Votes: +1

Jordan said:

How does $host become misleading? I bet function overloading will be a part of PHP 6 though.
 
report abuse
vote down
vote up
June 29, 2008 | url
Votes: -1

John said:

When using three parameters, $host is indeed the host server. However, when using a single parameter, $host is a data source name (not an actual host server address).

I couldn't find anything about overloading being part of PHP 6. Although, I am sure it will be implemented in soon-to-come versions. At least we get the long awaited namespaces with php 5.3.
 
report abuse
vote down
vote up
June 29, 2008
Votes: +0

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy