I have an abstract class for moving data from one database to another, and sometimes the data required to create the basic entries is different, due to the presence of a legacy table in the destination database which includes instructions for locating the data in the source. Obviously simplified, here is where the problem comes into play:
abstract class foo
{
protected abstract function createBaseEntry($id);
}
Sometimes, I only need the one ID passed to this function, but in some cases I need to pass two. Of course, if the actual method signature of the concrete method does not match the abstract method PHP will raise a Fatal error and halt execution. Other than predefining with null the maximum number of arguments and modifying every concrete class that extends this one, is there any way to get around this?
You could use
func_num_args()
andfunc_get_args()
to determine the number of arguments at runtime if you cannot/want not change the method signature to have an optional second argument.Example
Output (codepad)
This will also work when there are arguments defined in the signature.