PHP OO - How to Establish Class Relations between X Has Many Y

So, I have 2 classes: Lets call them Person and Car

I need to access some attributes from my person object in my Car class before I can instantiate.

I'm just saying something in order: $car = new Car($person);
 If so, how do I access these attributes of an object in the Car class? It would be like this:

class Car{

    function __construct($person)
    {  
        $this->person = $person;  
    }

}

If not, what would be the way to achieve this?

+4
source share
2 answers

We have some confusion here. OOP of the real world, consider and compare:

A vehicle is listed for or purchased by a person:

$person->addCar($car);

A ( ):

$car->addPerson($person, Car::SEAT_FRONT_LEFT);

Car::SEAT_FRONT_LEFT public const Car.

, .

-

, (Aware -) Interface.

, , , :

interface Person {
    public function getHeight();
    public function setHeight();
}

class CarOwner implements Person {
    protected $height;
    protected $cars = array();

    public function getHeight();
    public function setHeight();

    public function getCars();
};

class Driver implements Person  {
    protected $height;

    public function getHeight();
    public function setHeight();
};

class Car {
    protected $persons = array(); // @var array
    protected $readyToDrive = false;

    public const SEAT_FRONT_LEFT  = 1;
    public const SEAT_FRONT_RIGHT = 2;
    // ...

    public function addPerson(Person $person, $seat) {
        if ($person instanceof Driver) {
            $this->isReadyToDrive(true);
        }

         // I know this is stupid but you get the point:
        if ($this->getDriver()->getHeight() < 140 && $this->getSeat()->getPosition() > 40) {
            throw new Exception('please set the seat to fit your height!');
        }

        if (!in_array($person, $this->persons)) {
            $this->persons[$seat] = $person;
        }
        else {
            throw new Exception('person is already in the car!');
            // Person first needs to "leave" the car! like:
            // $car->removePerson($person)->addPerson($person, Car::SEAT_REAR_LEFT);
        }
    }


   public function getDriver() {
       if (isset($persons[self::SEAT_FRONT_LEFT]) && $persons[self::SEAT_FRONT_LEFT] instanceof Driver) {
           return persons[self::SEAT_FRONT_LEFT];
       }
       else { //... }
   }
};
+1

, . , , ? , Service/Controller, ?

function processSomething(Person $person, Car $car) {}

. . , , .

, :

public function __construct(array $personList) {
    //Check for a person
    if (count($personList) < 1) {
        throw new Exception('No person given');
    }

    //Validate list
    foreach ($personList as $person) {
        if (!$person instanceof Person) {
            throw new Exception('This is not a person');
        }
    }

    //Keep persons
    $this->personList = $personList;
}

: , ContainerObject:

class ContainerObject {
    public $person;
    public $moreInfo;
    public $manyMoreInfo;
}

class Person {
    public function sayHello() {};
}

class Car {
    private $containerObj;

    function __construct(ContainerObject $obj) {
        $this->containerObj= $obj;
    }

    function foo() {
        $this->containerObj->person->sayHello();
    }
}
0

All Articles