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?
func_get_args(). However, an associative array still seems like a better optionerror_reportingfunction still only accepts one parameter, I've updated my answer to explain more.