[PHP]Overloaded Functions Work Around?

All is well...
Loyal Member
Joined
Feb 22, 2006
Messages
1,520
Reaction score
0
I want to overload a few functions but since php doesn't support it I was wondering if there was a work around where if I don't set a parameter it will only run a certain portion of the function or something along those lines.
Any ideas on how to do this?

Example of what I wish I could do:
PHP:
<?php
function foo($str){
  echo "Only one paramter is set";
}
function foo($str,$str2){
  echo "Both parameters are set";
}
?>
 
PHP:
function foo($str, $str2 = false, $str3 = false)
{
    if ($str2 !== false)
    {
        if ($str3 !== false)
        {
            [if all 3 parameters are set]
        }

        [if only 2 are set]
    }

    [if only 1 parameter is set]
}

foo("hi"); //works
foo("hi", "this", "works"); // works

If you do not provide the second or third parameter, these will be 'false' ;).
 
Okay sorry I didn't elaborate enough =p
Here's a bit more in the function I actually plan on using it for:
PHP:
function foo($name,$action){
return "<form id='$name' method='post' action='$action'></form>";
}
function foo($name,$type,$action){
return "<form id='$name' method='$type' action='$action'></form>";
}
function foo($name,$type,$content,$action){
 return "<form id='$name' method='$type' action='$action'>$content</form>";
}
No here's the catch. What if you don't have content or actions...
I don't want want to have to write checks in every function to see if one of the parameters is set and I don't want to have to make up a variable each time I run the function.
 
Not sure do you mean this, and I'm quite long never touch PHP programming.
So, give it a try..
PHP:
function foo($name,$type,$content,$action)
{
	if (empty($name)) {
    	$name = "name";
    }
    if (empty($type)) {
    	$type = "type";
    }
    if (empty($content)) {
    	$content = "content";
    }
    if (empty($action)) {
    	$action = "post";
    }
    return "<form id='".$name."' method='".$type."' action='".$action."'>".$content."</form>";
}
 
Okay well it's this isn't an overloaded function like in the traditional sense.

Basically I want it to choose a function based on the number of parameters entered.
 
So you basically want to pass an unlimited ammount of parameters to a function? And based on the number of parameters you call another function?
What about this:

PHP:
function bar($array)
{
    switch (count($array))
    {
        case 2:
        {
             foo2();
             break;
        }
    }
}

So that you basically give an array as parameter and than decide what to do...

You can even put eval() in the code.

So instead of the switch:

PHP:
eval('foo'.count($array).'();');

Hope this is what you're looking for...
 
no no no!

PHP:
http://dk2.php.net/manual/en/function.func-num-args.php

function foo($name,$action,$type,$content)
{
    switch(func_num_args())
    {
        case 2:
            return "<form id='$name' method='post' action='$action'></form>";
        break;
        case 3:
            return "<form id='$name' method='$type' action='$action'></form>";
        break;
        case 4:
            return "<form id='$name' method='$type' action='$action'>$content</form>";
        break;
    }
}

PHP: func_num_args - Manual

Please, DO read the manual before you make homemade solutions.
You can even put eval() in the code.
And you can also commit suicide by jumping of a brigde. Both ideas are very stupid.

PHP does not support overloading as he asks, only preset paramters.

But the best solution is this one:
PHP:
function foo($name,$action,$type='post',$content='')
{
    return "<form id='$name' method='$type' action='$action'>$content</form>";
}
 
Hmm wonder why I didn't think about that =p
Oh well counting the parameters should work. Also, setting default parameters really doesn't do what I ask, sorry it was getting tricky to explain.
I've been getting use to Java and it throws off everything else. Stupid school.
 
Back