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:
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:
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:
Using this, I can redesign my connect() function to look something like:
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.
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 redeclareclass:: 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:

- function connect($host, $username=null, $password=null)
- {
- if($username == null && $password == null) {
- //parse the dsn
- } else {
- mysql_connect($host, $username, $password);
- }
- }
function connect($host, $username=null, $password=null)
{
if($username == null && $password == null) {
//parse the dsn
} else {
mysql_connect($host, $username, $password);
}
}
{
if($username == null && $password == null) {
//parse the dsn
} else {
mysql_connect($host, $username, $password);
}
}
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:

- function foo($arg1, $arg2) {
- }
- foo($bar); //this will cause issues
- foo($bar, $bat, $baz); //this will NOT cause any issues
function foo($arg1, $arg2) {
}
foo($bar); //this will cause issues
foo($bar, $bat, $baz); //this will NOT cause any issues
}
foo($bar); //this will cause issues
foo($bar, $bat, $baz); //this will NOT cause any issues

- function connect() {
- if(func_num_args() == 3) {
- mysql_connect($func_get_arg[0], $func_get_arg[1], $func_get_arg[2]);
- return;
- }
- //use dsn
- $args = parseDSN($func_get_arg[0]);
- mysql_connect($args[0], $args[1], $args[2]);
- }
function connect() {
if(func_num_args() == 3) {
mysql_connect($func_get_arg[0], $func_get_arg[1], $func_get_arg[2]);
return;
}
//use dsn
$args = parseDSN($func_get_arg[0]);
mysql_connect($args[0], $args[1], $args[2]);
}
if(func_num_args() == 3) {
mysql_connect($func_get_arg[0], $func_get_arg[1], $func_get_arg[2]);
return;
}
//use dsn
$args = parseDSN($func_get_arg[0]);
mysql_connect($args[0], $args[1], $args[2]);
}
Set as favorite
Bookmark
Email This
Hits: 180
Trackback(0)
Comments (4)

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
|
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
|
Write comment