1

I would like to create a function that can accept multiple, optional, and additive values in a parameter.

For example, I want to have a class

class Pizza {
   public function addToppings($additional_toppings) 
   {
   }
}

which I can use in my program as

$pizza1=new Pizza;
$pizza1->addToppings(SALAMI + HAM + BACON + ADDITIONAL_CHEESE);

$pizza2=new Pizza;
$pizza2->addToppings(HAM + PINEAPPLE + SAUSAGE);

where toppings can be of any combination and the choices for the topics fall under a predefined set.

How do you implement the addToppings function?

I've seem to remember seeing something similar in PHP but I can't remember which.

On the other, would you recommend associate arrays instead?

$pizza1->addToppings(array('salami','ham','bacon','additional_cheese'));
$pizza2->addToppings(array('ham','pineapple','sausage'));

This option seems simpler, but I want to get your ideas which you will choose and why. Thanks

EDIT:

I now remember one such PHP implementation.

The error_reporting function allows specifying multiple, additive error level constants as the parameter such as:

error_reporting(E_ERROR | E_WARNING | E_PARSE);
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

So is the implementation of error_reporting() allowed only since it is part of PHP but cannot be recreated otherwise?

4
  • The only way is indeed an array. PHP does not have unlimited param input like Java or Ruby for example. EDIT: Okay, there is a way actually; func_get_args(). However, an associative array still seems like a better option Commented Apr 3, 2014 at 8:37
  • Thanks Gerben. But why does it have to be associative and not simply indexed array as was my example? Commented Apr 4, 2014 at 6:29
  • The error_reporting function still only accepts one parameter, I've updated my answer to explain more. Commented Apr 4, 2014 at 7:19
  • It can be an indexed array yes. I was just thinking of simple strings when I typed that. Commented Apr 4, 2014 at 8:03

1 Answer 1

2

You can do it the way you suggested, passing an array, and my personal preference would be to do it that way. However PHP does support variable argument lists.

See: http://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list

Using variable arguments, you can make use of the func_num_args() and func_get_args() functions.

Here is an example:

<?php

addToppings('Cheese', 'Ham', 'Peppers');

function addToppings()
{
    echo "Added " . func_num_args() . " toppings<br />";
    var_dump(func_get_args());
}

?>

EDIT to include reply to OP edit regarding error_reporting() function

The error_reporting() function still only accepts one parameter, the pipe symbol between the error constants is a bitwise operator, so they evaluate to one single value. You could potentially implement something similar for your function but it does not seem practical with the pizza analogy.

More information on bitwise operations can be found on Wikipedia.

Sign up to request clarification or add additional context in comments.

4 Comments

Bad idea. You should avoid such definitions - because it's unreadable and makes function definition ambiguous
Yes, I state that I would prefer passing an array, however the question asks if there is an alternative, I just provided it. Your reasons for it being a bad idea are completely valid and I agree 100% +1
Thanks AJReading. You've also helped distinguish between having multiple values in one parameter (an array as you suggested) and having multiple, variable parameters retrieved by func_get_args. A helpful distinction to consider when we design functions.
Thanks for the clarification on error_reporting. Somehow I missed that and actually find bitwise operator quite complex (for my taste). Will stick with the array input suggestion

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.