PHP: Can I declare an abstract function with a variable number of arguments? PHP: Can I declare an abstract function with a variable number of arguments? php php

PHP: Can I declare an abstract function with a variable number of arguments?


In PHP 5.6 and later, argument lists may include the ... token to denote that the function accepts a variable number of arguments.

You can apply this to an abstract class as follows:

abstract class AbstractExample {    public function do_something(...$numbers);}

The arguments will be passed into the given variable as an array.


According the comment

I specifically want multiple, named arguments as it makes the code more readable.

abstract public function foo ($a, $b=null, $c=null);

If you want to pass an arbitrary number of values, use arrays

abstract public function foo ($args);

You should avoid "unknown number of arguments", because it makes things more difficult, then necessary: method signatures in interfaces as well as abstract methods should give the user a hint, how the method will work with any implementation. Its an important part, that the user shouldn't need to know anything about the implementation details. But when the number of arguments changes with every implementation, he must know, how the concrete methods are implemented.