D
D
driverx182019-07-17 16:30:19
PHP
driverx18, 2019-07-17 16:30:19

Which of these objects belongs to whom?

There are three classes, for example:
Pilot, Aircraft and Engine. What will be correct: or In fact, if I want to give a command to start the engine, then rather it should be like this: The pilot in the plane presses the power button , or, the pilot is not needed here, and in general it will be correct: ?
$pilot = new Pilot($airplane);
$airplane = new Airplane($pilot);
$pilot->airplane->engine->enable();
$airplane->engine->enable();

Answer the question

In order to leave comments, you need to log in

4 answer(s)
U
user49981, 2019-07-17
@user49981

$airplane = new Airplane();
$airplane->fly();

class Airplayne
{
    public function __construct(EngineInterface $engine) {}
    
    public function fly()
    {
        $this->engine->enable();
    }
    
    public function addPilots() {}
}

T
tsarevfs, 2019-07-17
@tsarevfs

You should not produce entities unnecessarily. If in doubt whether a pilot is needed or not, then most likely he is not needed.
A pilot owning a plane, the situation is a bit more exotic in my opinion. If you take the game simulator, it seems logical that the pilot is in the coordinate system of the aircraft and not vice versa.

A
Anton Shamanov, 2019-07-17
@SilenceOfWinter

depends on the context: if we are talking about aircraft systems, then the pilot and the engine are its components, and if we are talking about the pilot's console, then the aircraft and its engine. and this relationship does not have to be one-way + the classes themselves should not be strictly limited to such relationships.

T
ThunderCat, 2019-07-17
@ThunderCat

Figase the wisdom of the ages ...
IMHO it makes no sense to introduce the dependence of these components, and the interactions between them can either be transferred to the controller, or create a control class, such as a P (explored) aircraft / UAV, and there already introduce the interaction of the components, because the age / personal account of the pilot as if it can be involved in other logic and should not be associated with the engine.

or, the pilot is not needed here, and in general it will be correct:
xs how you build the logic there, maybe the engine is not needed as a separate class, everything at the level of methods and properties of the aircraft can be implemented: $airplane->startEngine();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question