Answer the question
In order to leave comments, you need to log in
How to guarantee the existence of a method in the heir without binding to arguments?
There is a base class MyBase. My goal is to ensure that myMethod is declared in every descendant, but I need freedom in declaring the arguments.
Those. i need something like the following
abstract class MyBase
{
abstract public function myMethod();
}
class Foo extends MyBase
{
public function myMethod(string $s)
{
//
}
}
Answer the question
In order to leave comments, you need to log in
There are no method overloads in PHP. It is better to think over the architecture correctly so that the set of arguments is stable at the level of the abstract class / interface than to pass a variable number of arguments to the method and then look for the causes of bugs and a reason for refactoring. Also, a variable number of arguments makes it tempting to give the method extra powers, smearing its original purpose.
At the class constructor level, it is possible to pass classes through dependency injection for logging, outputting information to the console, then they do not need to be passed in the run method, but can be used inside the method.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question