If I have a class,
abstract class Parent {
abstract function foo();
}
class Child extends Parent {
function foo($param) {
//stuff
}
}
I get an error because the abstract declaration doesn’t have any parameters but the child’s implementation of it does. I am making an adapter parent class with abstract functions that, when implemented, could have a variable amount of parameters depending on the context of the child class. Is there any structured way I can overcome this, or do I have to use func_get_args?
You have to use func_get_args if you want to have variable arguments in your function. Note that func_get_args gets all the arguments passed to a PHP function.
You can however enforce a minimum number of arguments to be passed to the function by including corresponding parameters to them.
For example:
Say that you have a function that you wish to call with at least 1 argument. Then just write the following:
Now that you have this definition of foo(), you have to at least call it with one argument. You can also choose to pass a variable number of arguments n where n > 1, and receive those arguments through func_get_args(). Keep in mind that $arg_list above will also contain a copy of $param as its first element.