Answer the question
In order to leave comments, you need to log in
How do you implement common functions/methods?
Depending on which page the user enters, the desired controller and model are connected. It often happens that in some controller a certain method is already implemented, which, for example, is received by the browser / OS and this method is suddenly required in another controller. Do not copy this method? I try to avoid even the slightest repetition of the code. I came to the conclusion that we need to add a new, common controller (there are, by the way, common methods). Would like to know how to properly implement the common part?
Answer the question
In order to leave comments, you need to log in
Implemented through a generic abstract controller class.
abstract class AbstractController
{
public function commonMethod() { }
}
class ConcreteControllerA extends AbstractController { }
class ConcreteControllerB extends AbstractController { }
trait ControllerTrait
{
public function traitMethod() { }
}
/**
* В этом контроллере будут общие методы абстрактного
* класса и методы трейта.
*/
class ConcreteControllerA extends AbstractController
{
use ControllerTrait;
}
/**
* А в этом только общие методы абстрактного класса.
*/
class ConcreteControllerB extends AbstractController
{
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question