Answer the question
In order to leave comments, you need to log in
How to control the object type being created in a method of another class?
For example, we have a Maker class
Class MyClass
{
public function __construct(SomeClass $obj, AnotherClass $obj2)
{
// инициализация с переданными параметрами
}
private function doSomething()
{
$event = new ConcreteEvent();
// И дальше что то делаем
}
}
Answer the question
In order to leave comments, you need to log in
Pattern Strategy
We make a common interface for Event objects (I came up with the begin() method out of my head):
interface EventInterface
{
public function begin();
}
class ConcreteEvent implements EventInterface
{
public function begin()
{
// ..
}
}
class OtherEvent implements EventInterface
{
public function begin()
{
// ..
}
}
Class MyClass
{
private $event;
public function __construct(SomeClass $obj, AnotherClass $obj2)
{
// инициализация с переданными параметрами
}
public function setEvent(EventInterface $event)
{
$this->event = $event;
}
private function doSomething()
{
// Работаем с нашим event
$someDoing = $this->event->begin();
}
}
$myObj = new MyClass(SomeClass $obj, AnotherClass $obj2);
// Set Event
$myObj->setEvent(new OtherEvent);
// Далее работаем с наши готовым объектом, у которого уже есть нужны Event
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question