1

Excuse my newbishness. While working on an ASP.Net based website, I remember seeing some methods in which could accept a different number of arguments. Specifically, they would do different things, depending on the number of parameters passed to them.

For example:

Email.sendEmail(address,subject,body);

would do something different to:

Email.sendEmail(address,bccaddresses,subject,body);

Is it possible for methods in PHP to do something similar? What is this thing called? Or have I just totally misremembered something?

7
  • 1
    This is called method/function overloading. PHP does not have this natively without hacky workarounds involving func_get_args or confusing variable names. Boo :-( Commented Feb 17, 2012 at 16:36
  • @cbuckley PHP seems to have an even better way of overloading . . . stackoverflow.com/a/9332030/798818 Commented Feb 17, 2012 at 16:37
  • Overloading != optional parameters. This allows for different arity but checking of data types must be handled in the function rather than explicitly defining multiple function signatures. Commented Feb 17, 2012 at 16:38
  • @cbuckley That's it! I thought it was called "overloading", but I couldn't find a good example of it, for some reason. Commented Feb 17, 2012 at 16:40
  • 1
    I would agree with Tim on this one, as it also allows for extensibility without breaking E_STRICT compliance. Commented Feb 17, 2012 at 17:28

4 Answers 4

3
function sendEmail($address, $bccaddresses, $subject, $body=NULL) {
  if ($body === NULL) {
     $bccaddresses=""; //or whatever default you want
     //shift arguments:
     $subject = $bccaddresses; $body = $subject;
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 This was what I was referring to as confusing variable names :-) Works well but can be hellish without good documentation!
+1 This does look like it could work, but I'll go with your recommendation, cbuckley.
2

You could use an associative array as the argument, and make use of extract().

function sendMail($opts){
    extract($opts, EXTR_SKIP); // EXTR_SKIP prevents extract from overwriting anything already in the symbol table
    if(isset($bccaddresses)){
        // do something with the bcc addresses
    }
    echo "sending some mail to $to with subject $subject";
}

sendMail(array("to"=>"[email protected]", "subject"=>"Hi!"));

This lets you pass in the arguments as named values, with optional values as you see fit.

Comments

1

You are searching for func_num_args() and func_get_args().

See also related examples, it shows you how to do exactly what you need.

Comments

1

In PHP, you set default values for functions.

function myFunc($val1 = 1, $val2 = 2, $val3 = 3){
    echo "$val1 $val2 $val3";
}


myFunc();     // outputs 1 2 3
myFunc(5);    // outputs 5 2 3
myFunc(8, 1); // outputs 8 1 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.